0

I created a new QWidget with a single button in Qt Designer and i added this to the source code :

void MainWindow::startAnimation()
{
    QPropertyAnimation animation(ui->pushButton, "geometry");
    animation.setDuration(3000);
    animation.setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));
    animation.setEndValue(QRect(this->x(),this->y(), 10,10));
    animation.setEasingCurve(QEasingCurve::OutBounce);
    animation.start();
}

void MainWindow::on_pushButton_clicked()
{
    startAnimation();
}

When I click on the button, it disappears and it doesn't animate.

Alex L.
  • 813
  • 2
  • 17
  • 34

1 Answers1

2

Your animation gets out of scope and is automatically deleted at the end of the startAnimation() function. That's why nothing happens. Create the QPropertyAnimation instance with new and delete it later using finished signal and deleteLater slot, like this:

void MainWindow::startAnimation()
{
    QPropertyAnimation* animation = new QPropertyAnimation(ui->pushButton, "geometry");
    ...
    connect(animation, SIGNAL(finished()), animation, SLOT(deleteLater()));
    animation->start();
}
  • You don't have to make a connection to delete the animation when it's finished. Just set the deletion policy to QAbstractAnimation::DeleteWhenStopped when calling [start()](http://qt-project.org/doc/qt-4.8/qabstractanimation.html#start). `animation->start(QAbstractAnimation::DeleteWhenStopped)` – thuga May 27 '13 at 08:04