Results 1 to 5 of 5

Thread: Avoid flicker during slideshow with QPropertyAnimation

  1. #1
    Join Date
    Dec 2010
    Posts
    18
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Avoid flicker during slideshow with QPropertyAnimation

    I hace a fullscreen program with several screens, and I want to go for one screen to the next one witht an slideshow animation.

    I have a fullscreeen QMainWidget, with a placeholder VerticalLayout, where I put different QWidgets that act as "screens" for my application, one at a time. The user interacts with the current screen, and when it's time to go to the next one, I remove the current page from the vertical layout and place the new page instead.

    It works flawlessly, but I want to spice it up with some QPropertyAnimation. I want to transition from one page to the next one with a little slideshow animation. I'm using the "geometry" property, to move both the old page and the new page, right-to-left. This is my code:

    Qt Code:
    1. if (m_currentPage && animate)
    2. {
    3. // Don't allow the user to interact with the old page as it's being carried away
    4. m_currentPage->setEnabled(false);
    5.  
    6. QRect a = ui->placeholder->geometry();
    7. QRect b = QRect(a.width(), a.y(), a.width(), a.height());
    8. QRect c = QRect(-a.width(), a.y(), a.width(), a.height());
    9.  
    10. int animationDuration = 400;
    11.  
    12. QPropertyAnimation *animation = new QPropertyAnimation(m_currentPage, "geometry", this);
    13. animation->setStartValue(a);
    14. animation->setEndValue(direction == Forward ? c : b);
    15. animation->setDuration(animationDuration);
    16. animation->setEasingCurve(QEasingCurve::OutQuad);
    17. animation->start();
    18. connect(animation, SIGNAL(finished()), m_currentPage, SLOT(deleteLater()));
    19.  
    20. QPropertyAnimation *animation2 = new QPropertyAnimation(newPage, "geometry", this);
    21. animation2->setStartValue(direction == Forward ? b : c);
    22. animation2->setEndValue(a);
    23. animation2->setDuration(animationDuration);
    24. animation2->setEasingCurve(QEasingCurve::OutQuad);
    25. animation2->start();
    26. connect(animation2, SIGNAL(finished()), newPage, SLOT(setFocus()));
    27. }
    28. else
    29. {
    30. // Remove previous page
    31. clearLayout(ui->placeholder);
    32. }
    33.  
    34. // Set new page
    35. newPage->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    36. ui->placeholder->addWidget(newPage);
    37. newPage->setFocus();
    38.  
    39. connect(newPage,
    40. SIGNAL(setPage(KioskPage*,KioskMainWindow::Direction)),
    41. SLOT(setPage(KioskPage*,KioskMainWindow::Direction)));
    42. newPage->init();
    43.  
    44. m_currentPage = newPage;
    45.  
    46. languageChange();
    47.  
    48. if (animate)
    49. {
    50. newPage->hide();
    51. QTimer::singleShot(100, newPage, SLOT(show()));
    52. }
    To copy to clipboard, switch view to plain text mode 

    It works fairly well, but has one annoying bug: after this code is run, the function quits, the control returns to Qt internals and windows start to be drawn, what I see is, first, the newPage flickers once all over the m_currentPage, just for a fraction of a second, and then it disappears and the animation starts smoothly. I've tried a lot of stuff here, and that ugly QTimer::singleShot(100, newPage, SLOT(show())); is the only thing that appears to at least disguise part of the problem, but not completely. As soon as the show() slot is called, newPage flickers once over m_currentPage, disappears and appears again where it should be according to the QPropertyAnimation.

    Does anyone know why this is happening, and what I could do to avoid it?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Avoid flicker during slideshow with QPropertyAnimation

    Please provide a minimal compilable example reproducing the problem.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Dec 2010
    Posts
    18
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Avoid flicker during slideshow with QPropertyAnimation

    Here you have
    Attached Files Attached Files

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Avoid flicker during slideshow with QPropertyAnimation

    In my opinion the problem is with your "placeholder" layout. If you want to animate a page, it can't be part of a layout.

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class Widget : public QWidget {
    4. Q_OBJECT
    5. public:
    6. Widget(QWidget *parent = 0) : QWidget(parent) {
    7. QVBoxLayout *l = new QVBoxLayout(this);
    8. placeholder = new QWidget;
    9. placeholder->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    10. l->addWidget(placeholder);
    11. l->addWidget(b);
    12. b->setText("click");
    13. connect(b, SIGNAL(clicked()), this, SLOT(nextPage()));
    14. current = 0;
    15. }
    16. public slots:
    17. void nextPage() {
    18. QWidget *newPage = new QLabel("page");
    19. newPage->setAutoFillBackground(true);
    20. QStringList c = QColor::colorNames();
    21. QPalette p = newPage->palette();
    22. p.setColor(QPalette::Window, QColor(c.at(qrand() % c.size())));
    23. newPage->setPalette(p);
    24. newPage->setParent(placeholder);
    25. QPropertyAnimation *anim = new QPropertyAnimation(newPage, "geometry", newPage);
    26. QRect start = placeholder->rect();
    27. start.setTopLeft(start.topRight());
    28. newPage->setGeometry(start);
    29. anim->setStartValue(start);
    30. anim->setEndValue(placeholder->rect());
    31. anim->start();
    32.  
    33. if(current) {
    34. QPropertyAnimation *anim = new QPropertyAnimation(current, "geometry", current);
    35. anim->setStartValue(placeholder->rect());
    36. QRect r = placeholder->rect();
    37. r.translate(-r.width(), 0);
    38. anim->setEndValue(r);
    39. connect(anim, SIGNAL(finished()), current, SLOT(deleteLater()));
    40. anim->start();
    41. }
    42.  
    43. current = newPage;
    44. current->show();
    45. }
    46. private:
    47. QWidget *placeholder;
    48. QWidget *current;
    49. };
    50.  
    51. #include "main.moc"
    52.  
    53. int main(int argc, char **argv) {
    54. QApplication app(argc, argv);
    55. Widget w;
    56. w.show();
    57. return app.exec();
    58. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    RazZziel (6th March 2012)

  6. #5
    Join Date
    Dec 2010
    Posts
    18
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Avoid flicker during slideshow with QPropertyAnimation

    This pitiful mortal shall bow before you, oh wise Master Of Zen.

Similar Threads

  1. Replies: 2
    Last Post: 21st February 2012, 11:13
  2. How to apply the turnover effect to the image slideshow
    By Sirisha in forum Qt Programming
    Replies: 2
    Last Post: 22nd November 2011, 07:16
  3. Replies: 4
    Last Post: 16th April 2011, 10:19
  4. Flicker Problem
    By ToddAtWSU in forum Qt Programming
    Replies: 1
    Last Post: 26th June 2006, 16:50
  5. QDockWidget flicker
    By fellobo in forum Qt Programming
    Replies: 1
    Last Post: 28th April 2006, 20:42

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.