延迟启动 QPropertyAnimation
Start QPropertyAnimation delayed
我有一个小动画 showing/hiding 当鼠标悬停在父窗口小部件上时的一个帧(在下面的代码片段中 "MyWidget")。
动画只是改变了框架的最大宽度 属性,因此框架变得可见 "slide-in effect"。 (框架本身放置在网格布局中。)
我的问题是如何延迟启动动画?示例:在鼠标离开事件后500ms开始,所以滑出效果是延迟的,没有立即开始。
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();
}
Mezzo 的提示是针对目标 - 再次感谢! - 但我插入了一个检查器以避免 'flicker effect'。 (当 slideIn 效果仍为 运行 时,始终等待静态的毫秒数会导致异步 slideOut 效果。)
可能对答案感兴趣的人。 (我还修复了避免在每个动画触发器中分配的内存泄漏):
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;
}
我有一个小动画 showing/hiding 当鼠标悬停在父窗口小部件上时的一个帧(在下面的代码片段中 "MyWidget")。
动画只是改变了框架的最大宽度 属性,因此框架变得可见 "slide-in effect"。 (框架本身放置在网格布局中。)
我的问题是如何延迟启动动画?示例:在鼠标离开事件后500ms开始,所以滑出效果是延迟的,没有立即开始。
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();
}
Mezzo 的提示是针对目标 - 再次感谢! - 但我插入了一个检查器以避免 'flicker effect'。 (当 slideIn 效果仍为 运行 时,始终等待静态的毫秒数会导致异步 slideOut 效果。)
可能对答案感兴趣的人。 (我还修复了避免在每个动画触发器中分配的内存泄漏):
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;
}