QPropertyAnimation不起作用

时间:2017-01-21 23:33:13

标签: c++ qt

我正在尝试使用Qt的动画框架。我正在使用Qt网站上的一些示例代码,但它不起作用,错误:setGeometryDp: Unable to set geometry 100x30+0+0 on QWidgetWindow/'QPushButtonClassWindow'. Resulting geometry: 120x30+0+0 (frame: 8, 31, 8, 8, custom margin: 0, 0, 0, 0, minimum size: 0x0, maximum size: 16777215x16777215).

QPushButton button("Animated Button");
button.setGeometry(0, 0, 100, 30);
button.show();

QPropertyAnimation animation(&button, "geometry");
animation.setDuration(10000);
animation.setStartValue(QRect(0, 0, 100, 30));
animation.setEndValue(QRect(250, 250, 100, 30));

animation.start(); 

我不知道我做错了什么。

1 个答案:

答案 0 :(得分:0)

对象超出范围

这有效:

QPushButton *button = new QPushButton();
button->setGeometry(0, 0, 100, 30);
button->show();

QPropertyAnimation *animation = new QPropertyAnimation(button, "geometry");
animation->setDuration(10000);
animation->setStartValue(QRect(0, 0, 100, 30));
animation->setEndValue(QRect(250, 250, 100, 30));

animation->start();

编辑: 我的最终解决方案

QPushButton *button = new QPushButton(this);
button->setGeometry(0, 0, 100, 30);
button->show();

QPropertyAnimation *animation = new QPropertyAnimation(button, "geometry");
animation->setDuration(10000);
animation->setStartValue(QRect(0, 0, 100, 30));
animation->setEndValue(QRect(250, 250, 100, 30));

connect(animation, SIGNAL(finished()), animation, SLOT(deleteLater()));

animation->start();