Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Forum Updated on Feb 6th

    Solved How to detect when animation completes

    General and Desktop
    2
    7
    1902
    Loading More Posts
    • 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
      ChajusSaib last edited by ChajusSaib

      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 Reply Quote 0
      • Chris Kawa
        Chris Kawa Moderators last edited by Chris Kawa

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

        connect(animation, &QPropertyAnimation::finished, button->parentWidget(), &QWidget::deleteLater);
        
        1 Reply Last reply Reply Quote 1
        • C
          ChajusSaib last edited by

          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 Reply Quote 0
          • Chris Kawa
            Chris Kawa Moderators last edited by

            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 Reply Quote 0
            • C
              ChajusSaib @Chris Kawa last edited by

              @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 Reply Quote 0
              • Chris Kawa
                Chris Kawa Moderators last edited by 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 1 Reply Last reply Reply Quote 1
                • C
                  ChajusSaib @Chris Kawa last edited by

                  @Chris-Kawa I feel stupid. Thank you!

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post