如何使用QPropertyAnimation在幻灯片放映期间避免周期性闪烁/拖动效果

时间:2016-01-12 08:56:16

标签: qt slide flicker qpropertyanimation

QPropertyAnimation在动画期间产生周期性的闪烁/拖动效果,持续时间较长,约为1秒或2秒。对于小持续时间的动画(约500毫秒或更短),QPropertyAnimation可生成平滑动画,而不会产生特定的闪烁/拖动效果。这种闪烁/拖动效果大约每500毫秒出现一次。我需要尽快找到一些解决方案。我附上了重现问题的最小可编辑示例。请看看并帮忙。

我在Windows10中使用Qt5.5,我使用MinGW和Visual Studio 2013编译器,Core i5笔记本电脑。

#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();
}

0 个答案:

没有答案