我正在尝试在Qt桌面应用程序中测试动画。我只是复制了help中的示例。点击按钮后,新按钮只是出现在左上角,没有动画(甚至结束位置是错误的)。我是不是遗漏了什么?
Qt 5.0.1,Linux Mint 64位,GTK
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPropertyAnimation>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QPushButton *button = new QPushButton("Animated Button", this);
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();
}编辑:已解决。动画对象必须作为全局引用。例如,在私有QPropertyAnimation *动画部分。则QPropertyAnimation =新建(...);
发布于 2013-03-23 05:50:08
您只是没有复制这个示例,您还做了一些破坏它的更改。您的animation变量现在是在on_pushButton_clicked函数结束时销毁的局部变量。使QPropertyAnimation实例成为MainWindow类的成员变量,并按如下方式使用它:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow), mAnimation(0)
{
ui->setupUi(this);
QPropertyAnimation animation
}
MainWindow::~MainWindow()
{
delete mAnimation;
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QPushButton *button = new QPushButton("Animated Button", this);
button->show();
mAnimation = new QPropertyAnimation(button, "geometry");
mAnimation->setDuration(10000);
mAnimation->setStartValue(QRect(0, 0, 100, 30));
mAnimation->setEndValue(QRect(250, 250, 100, 30));
mAnimation->start();
}发布于 2013-03-23 14:58:40
您不需要专门为删除mAnimation变量创建一个插槽。如果您使用QAbstractAnimation::DeleteWhenStopped,Qt可以为您做到这一点
QPropertyAnimation *mAnimation = new QPropertyAnimation(button, "geometry");
mAnimation->setDuration(10000);
mAnimation->setStartValue(QRect(0, 0, 100, 30));
mAnimation->setEndValue(QRect(250, 250, 100, 30));
mAnimation->start(QAbstractAnimation::DeleteWhenStopped);https://stackoverflow.com/questions/15580171
复制相似问题