I'm trying to test animations in Qt desktop application. I just copied example from help. After button click, new button just appear in left top corner without animation (even end position is wrong). Am I missing something?
Qt 5.0.1, Linux Mint 64bit, 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();
}
Edit: Solved. Animation object must be as global reference. For example in section private QPropertyAnimation *animation. Then QPropertyAnimation = New(....);
You don't need to make a slot specifically for deleting the mAnimation
variable. Qt can do it for you if you use QAbstractAnimation::DeleteWhenStopped
:
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With