Results 1 to 4 of 4

Thread: Animate an hidden widget

  1. #1
    Join Date
    Apr 2010
    Posts
    22
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Animate an hidden widget

    Hi all,

    I'd like to animate (show and hide) a widget when a toggle button is pressed.
    I wrote this code:
    Qt Code:
    1. MainWindow::MainWindow
    2. {
    3. [...]
    4. //ui->widget->hide();//[1]
    5. //ui->button->setChecked(false);//[2]
    6. startgeometry = QRect(0,0,0,0);
    7. connect(ui->button,SIGNAL(toggled(bool)),this,SLOT(togglePyShell(bool)));
    8. }
    9.  
    10. void MainWindow::togglePyShell(bool show)
    11. {
    12. QRect formerGeometry = QRect(ui->widget->geometry());
    13. if(startgeometry==QRect(0,0,0,0)){
    14. startgeometry = formerGeometry;
    15. }
    16. if(show){
    17. //ui->widget->show();//[3]
    18. QPropertyAnimation *showAnimation = new QPropertyAnimation(ui->widget, "geometry");
    19. showAnimation->setDuration(100);
    20. showAnimation->setEasingCurve(QEasingCurve::Linear);
    21. showAnimation->setStartValue(formerGeometry);
    22. showAnimation->setEndValue(startgeometry);
    23. showAnimation->start(QPropertyAnimation::DeleteWhenStopped);
    24. }else{
    25. QPropertyAnimation *hideAnimation = new QPropertyAnimation(ui->widget, "geometry");
    26. hideAnimation->setDuration(100);
    27. hideAnimation->setEasingCurve(QEasingCurve::Linear);
    28. hideAnimation->setStartValue(formerGeometry);
    29. QPoint endTopLeftCorner = QPoint(ui->widget->pos() + QPoint(0, ui->widget->height()));
    30. QRect finalGeometry = QRect(endTopLeftCorner, QSize(ui->widget->width(), 0));
    31. hideAnimation->setEndValue(finalGeometry);
    32. hideAnimation->start(QPropertyAnimation::DeleteWhenStopped);
    33. //ui->widget->hide();//[4]
    34. }
    35. }
    To copy to clipboard, switch view to plain text mode 

    It works if the widget is visible after costructor (and button pressed), but I'd like to start with the widget invisible (and button not checked).

    I don't know how to hide the widget.
    If i try to uncomment from [1] up to [4] or subset of them I have strange behaviour, because (i guess) I can't save original widget's size into startgeometry.

    Any help is appreciated,
    Thanks in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Animate an hidden widget

    If you don't have a start geometry and you show, wouldn't the animation go from "0,0,0,0" to "current geometry"?

    Cheers,
    _

  3. #3
    Join Date
    Apr 2010
    Posts
    22
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Animate an hidden widget

    Maybe i didn't understand your reply.

    But if i remove startgeometry QRect and I show the widget I wouldn't have what I want: start with a hidden widget.

    On the other hand if I hide the widget (ui->widget->hide() ) how can I get final widget's geometry of widget (for "show transition")?

  4. #4
    Join Date
    Apr 2010
    Posts
    22
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Animate an hidden widget

    Thanks to anda_skoa I solved in this way, maybe it could be useful for someone else.

    Qt Code:
    1. void MainWindow::resizeEvent(QResizeEvent *)
    2. {
    3. //in case of window resize we have to hide widget so we'll fall in first case(see below) and set push button as unchecked
    4. if(ui->widget->isVisible()){
    5. ui->widget->setHidden(true);
    6. ui->button->setChecked(false);
    7. }
    8. }
    9.  
    10. void MainWindow::showWidget()
    11. {
    12. QRect fo;
    13. QRect fi;
    14.  
    15. //There are 2 cases:
    16. // 1. Widget was never shown before,
    17. // 2.Widget was shown (at least) once and then hide again,
    18. if(ui->widget->isHidden()){
    19. //If the widget is hidden then we're in the first case.
    20.  
    21. //We set up y coord of starting transition to a bigger value than height coord and all other
    22. //coords at the widget's coords; while we have to set up final transition coords to widget's coords.
    23. //In this way it'll look like upward motion.
    24.  
    25.  
    26. //PLEASE NOTE: before you have to perform ui->widget->show() and then get widget's size() and pos(), else you'll get wrong values.
    27.  
    28. ui->widget->show();
    29. QPoint pos = ui->widget->pos();
    30. QSize size = ui->widget->size();
    31.  
    32. fo = QRect(pos.x(),pos.y()+size.height(),size.width(),size.height());
    33. fi = QRect(pos,size);
    34. }else{
    35. //in second case: we have to set up start transition coords at actual coords of widget, as it's hidden
    36. //we would have y = y + height, and we set up y coord of final transition coords to y = y - height.
    37. fo = ui->widget->geometry();
    38. fi = QRect(fo.x(),fo.y()-fo.height(),fo.width(),fo.height());
    39. }
    40. QPropertyAnimation *showAnimation = new QPropertyAnimation(ui->widget, "geometry");
    41. showAnimation->setDuration(100);
    42. showAnimation->setEasingCurve(QEasingCurve::Linear);
    43. showAnimation->setStartValue(fo);
    44. showAnimation->setEndValue(fi);
    45. showAnimation->start(QPropertyAnimation::DeleteWhenStopped);
    46. }
    47.  
    48. void MainWindow::hideWidget()
    49. {
    50. //coords of starting transition must be set up at actual widget's coords, as it's visible; while final
    51. //transition coords must be set up with: y = y + height.
    52.  
    53. QRect fo = QRect(ui->widget->geometry());
    54. QRect fi = QRect(fo.x(),fo.y()+fo.height(),fo.width(),fo.height());
    55.  
    56. QPropertyAnimation *hideAnimation = new QPropertyAnimation(ui->widget, "geometry");
    57. hideAnimation->setDuration(100);
    58. hideAnimation->setEasingCurve(QEasingCurve::Linear);
    59.  
    60. hideAnimation->setStartValue(fo);
    61. hideAnimation->setEndValue(fi);
    62. hideAnimation->start(QPropertyAnimation::DeleteWhenStopped);
    63. }
    64.  
    65. void MainWindow::togglePyShell(bool show)
    66. {
    67. if(show){
    68. showWidget();
    69. }else{
    70. hideWidget();
    71. }
    72. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by AlbertoN; 10th October 2014 at 19:48.

Similar Threads

  1. Replies: 2
    Last Post: 30th January 2013, 19:06
  2. Replies: 0
    Last Post: 12th July 2011, 08:19
  3. Replies: 5
    Last Post: 30th July 2009, 12:37
  4. Ensuring a Widget Is Hidden
    By wswartzendruber in forum Qt Programming
    Replies: 10
    Last Post: 19th May 2009, 13:33
  5. how to animate a subclassed widget
    By babu198649 in forum Newbie
    Replies: 4
    Last Post: 31st December 2007, 13:07

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.