Search code examples
qtqt4qpropertyanimation

Start QPropertyAnimation delayed


I have a small animation showing/hiding a frame when the mouse hovers the parent widget (in the code snippet below "MyWidget").

The animation simply changing the maximumWidth property of the frame so the frame becomes visible as some "slide-in effect". (The frame itself is place in a grid layout.)

My Question is how to start the animation delayed? Example: Start 500ms after the mouse leaveEvent, so the slide-out effect is delayed and did not start immediately.

void MyWidget::enterEvent( QEvent * event )
{
    //slide-in effect
    QPropertyAnimation *animation = new QPropertyAnimation(ui.frame_buttons, "maximumWidth");
    animation->setDuration(1000);
    animation->setStartValue(ui.frame_buttons->maximumWidth());
    animation->setEndValue(100);
    animation->setEasingCurve(QEasingCurve::InOutQuad);

    animation->start();
}

void MyWidget::leaveEvent( QEvent * event )
{
    //slide-out effect
    QPropertyAnimation *animation = new QPropertyAnimation(ui.frame_buttons, "maximumWidth");
    animation->setDuration(1000);
    animation->setStartValue( ui.frame_buttons->maximumWidth() );
    animation->setEndValue(0);
    animation->setEasingCurve(QEasingCurve::InOutQuad);

    //delay start() for a small amount of time
    animation->start();
}

Solution

  • The hint from Mezzo was targeting - thanks again! - but I inserted a checker to avoid a 'flicker effect'. (Always waiting a static amount of milliseconds can cause an asynchronous slideOut effect, when the slideIn effect is still running.)

    For whom that may be interested in an answer. (I also fixed a memory leak in avoiding the allocation in each animation trigger):

    void MyWidget::enterEvent( QEvent * event )
    {
        //start from where the slideOut animation currently is
        m_slideInAnimation->setStartValue(ui.frame_buttons->maximumWidth());
        m_slideInAnimation->start();
    }
    
    void MyWidget::leaveEvent( QEvent * event )
    {
        //start from where the slideIn animation currently is
        m_slideOutAnimation->setStartValue( ui.frame_buttons->maximumWidth() );
    
        //start slide_out animation only if slide_in animation finish to avoid flicker effect
        if(ui.frame_buttons->maximumWidth() != m_slideInAnimation->endValue()) 
        {
            m_slideOutAnimation->start();
        }
        else
        {
            QTimer::singleShot(700, m_slideOutAnimation, SLOT(start()));
        }   
    }
    
    void MyWidget::createAnimations()
    {
        m_slideInAnimation= new QPropertyAnimation(ui.frame_buttons, "maximumWidth");
        m_slideInAnimation->setDuration(1000);
        m_slideInAnimation->setEndValue(100);
        m_slideInAnimation->setEasingCurve(QEasingCurve::InOutQuad);
    
        m_slideOutAnimation = new QPropertyAnimation(ui.frame_buttons, "maximumWidth");
        m_slideOutAnimation->setDuration(1000);
        m_slideOutAnimation->setEndValue(0);
        m_slideOutAnimation->setEasingCurve(QEasingCurve::InOutQuad);
    }
    
    void MyWidget::MyWidget()
    {
        this->createAnimations();
    }
    
    void MyWidget::~MyWidget()
    {
        delete m_slideInAnimation;
        delete m_slideOutAnimation;
    }