首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >启动QPropertyAnimation延迟

启动QPropertyAnimation延迟
EN

Stack Overflow用户
提问于 2015-11-02 14:13:30
回答 1查看 1.3K关注 0票数 0

当鼠标悬停父部件时,我有一个显示/隐藏框架的小动画(在“MyWidget”下面的代码片段中)。

动画简单地改变了框架的maximumWidth属性,使框架成为可见的某种“幻灯片效果”。(框架本身位于网格布局中。)

我的问题是如何启动动画延迟?示例:在鼠标leaveEvent之后启动500 is,因此滑出效果被延迟,并且没有立即启动.

代码语言:javascript
复制
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();
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-10 15:32:21

来自Mezzo的暗示--再次感谢!--但是我插入了一个跳棋,以避免“闪烁效应”。(当slideOut效果仍在运行时,总是等待静态毫秒会导致异步slideIn效果。)

可能对谁感兴趣的答案。(我还修复了一个内存漏洞,以避免在每个动画触发器中分配):

代码语言:javascript
复制
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;
}   
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33479629

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档