PDA

View Full Version : Avoid periodic flicker/drag during slideshow with QPropertyAnimation



shujaat
12th January 2016, 08:22
I have come across a problem/bug in QPropertyAnimation, QPropertyAnimation produce periodic flicker/drag effect during animation of longer duration of about 1 second or two. For small duration animation (about 500 ms or less) the QPropertyAnimation produce smooth animation without that specific flicker/drag effect. That flicker/drag like effect appears round about every 500ms. And i need to come across some solution very quickly. I have attached the minimal compilable example reproducing the problem. Please have a look and help.
(The code was originally posted by wysota (http://www.qtcentre.org/members/11-wysota) in response of the question by some other user on the forum for his own specific kind of problem)

I am using Qt5.5 in Windows10, Core i5 Laptop.


#include <QCoreApplication>
#include <QWidget>
#include <QVBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QPropertyAnimation>
#include <QApplication>


class Widget : public QWidget {
Q_OBJECT
public:
Widget(QWidget *parent = 0) : QWidget(parent) {
QVBoxLayout *l = new QVBoxLayout(this);
placeholder = new QWidget;
placeholder->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
l->addWidget(placeholder);
QPushButton *b = new QPushButton;
l->addWidget(b);
b->setText("click");
connect(b, SIGNAL(clicked()), this, SLOT(nextPage()));
current = 0;
}
public slots:
void nextPage() {
QWidget *newPage = new QLabel("page");
newPage->setAutoFillBackground(true);
QStringList c = QColor::colorNames();
QPalette p = newPage->palette();
p.setColor(QPalette::Window, QColor(c.at(qrand() % c.size())));
newPage->setPalette(p);
newPage->setParent(placeholder);
QPropertyAnimation *anim = new QPropertyAnimation(newPage, "geometry", newPage);
QRect start = placeholder->rect();
start.setTopLeft(start.topRight());
newPage->setGeometry(start);
anim->setStartValue(start);
anim->setEndValue(placeholder->rect());
anim->setDuration(4000);
anim->start();

if(current) {
QPropertyAnimation *anim = new QPropertyAnimation(current, "geometry", current);
anim->setStartValue(placeholder->rect());
QRect r = placeholder->rect();
r.translate(-r.width(), 0);
anim->setEndValue(r);
anim->setDuration(4000);
connect(anim, SIGNAL(finished()), current, SLOT(deleteLater()));
anim->start();
}

current = newPage;
current->show();
}
private:
QWidget *placeholder;
QWidget *current;
};

#include "main.moc"

int main(int argc, char **argv) {
QApplication app(argc, argv);
Widget w;
w.show();
return app.exec();
}