Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QPropertyAnimation doesn't work

Tags:

qt

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(....);

like image 799
Dibo Avatar asked Mar 22 '13 21:03

Dibo


1 Answers

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);
like image 73
Ville Lukka Avatar answered Sep 28 '22 09:09

Ville Lukka