2

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

0 回答 0