How to detect when animation completes



  • Hello there, I would like to add an animation when I delete a widget that contains QLineEdit and QPushButton.

    This animation works when I don't call deleteLater on the widget but then I get no way to delete the widget. I could setup a timer for 1 second and start that when the animation starts but I was wondering if there were any way for me to detect when the animation has completed.

    void ClassName::removeLine()
    {
        QPushButton *button = qobject_cast<QPushButton *>(sender()); // get the caller object(QPushButton)
        if (button)
        {
            QPropertyAnimation *animation = new QPropertyAnimation(button->parentWidget(), "maximumHeight");
            animation->setDuration(1000);
            animation->setStartValue(500);
            animation->setEndValue(0);
    
            animation->start();
            
            disconnect(button, SIGNAL(clicked(bool)), this, SLOT(removeLine()));    // disconnect
            button->parentWidget()->deleteLater();                                  // delete parent widget*/
        }
    }
    

    Thank you!


  • Moderators

    QPropertyAnimation inherits QAbstractAnimation which provides a finished signal. You can use that.

    connect(animation, &QPropertyAnimation::finished, button->parentWidget(), &QWidget::deleteLater);
    


  • I've done that and it's solved but for some reason there is a delay when I press the delete button.


  • Moderators

    Delay of what? The click? The animation? The object destruction?
    When posting questions please provide as many informations as possible. Crystal ball debugging is hard ;)



  • @Chris-Kawa Sorry, the delay is between pressing the button and the animation to start. When I remove the animation and press the button the widget is removed with no delay.

    So when I click the button, there is a bit of delay and then the animation starts. The delay is noticeable.

    Thank you


  • Moderators

    Well, buttons are not usually that high (500 in your code), so my guess is that the animation actually starts, but it takes time for the maximum height to reach the real height of the button.
    instead of hardcoding that value you could start from the actual height of the widget:

    animation->setStartValue(button->parentWidget()->height());
    


  • @Chris-Kawa I feel stupid. Thank you!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.