Alex L.
Alex L.

Reputation: 821

Button does not animate

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.

Upvotes: 0

Views: 894

Answers (1)

user362638
user362638

Reputation:

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

Upvotes: 2

Related Questions