Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to detect when animation completes

How to detect when animation completes

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 3.6k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    ChajusSaib
    wrote on last edited by ChajusSaib
    #1

    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!

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

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

      connect(animation, &QPropertyAnimation::finished, button->parentWidget(), &QWidget::deleteLater);
      
      1 Reply Last reply
      2
      • C Offline
        C Offline
        ChajusSaib
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          C 1 Reply Last reply
          1
          • Chris KawaC Chris Kawa

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

            C Offline
            C Offline
            ChajusSaib
            wrote on last edited by
            #5

            @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

            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by Chris Kawa
              #6

              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());
              
              C 1 Reply Last reply
              1
              • Chris KawaC Chris Kawa

                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());
                
                C Offline
                C Offline
                ChajusSaib
                wrote on last edited by
                #7

                @Chris-Kawa I feel stupid. Thank you!

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved