您的位置:首页 > 编程语言 > Qt开发

Qt之QPauseAnimation

2016-07-12 09:41 375 查看

简述

QPauseAnimation类为QSequentialAnimationGroup提供了一个暂停。

如果你想为QSequentialAnimationGroup动画之间添加延迟,可以插入一个QPauseAnimation。它没有任何动画,但当在指定的毫秒数之内开始运行时不会结束。可以通过构造函数指定暂停的时间,也可以通过setDuration()设置。

没必要自己建立一个QPauseAnimation,QSequentialAnimationGroup提供了便利的函数addPause()和insertPause(),这些函数可以简单地暂停应该持续的毫秒数。

简述

公共函数

示例
效果

源码

更多参考

公共函数

void setDuration(int msecs)

设置暂停的毫秒数。

暂停持续的时间不应该是负的,默认的时间是250毫秒。

示例

下面,我们通过QSequentialAnimationGroup来构建一个串行动画组,并添加属性动画QPropertyAnimation和暂停动画QPauseAnimation,这里也可以使用addAnimation()添加其它动画/动画组,就不予演示了。

效果



源码

MainWindow::MainWindow(QWidget *parent)
: CustomWindow(parent)
{
...

QPushButton *pStartButton = new QPushButton(this);
pStartButton->setText(QString::fromLocal8Bit("开始动画"));

QList<QLabel *> list;
QStringList strList;
strList << QString::fromLocal8Bit("一去丶二三里") << QString::fromLocal8Bit("青春不老,奋斗不止");

for (int i = 0; i < strList.count(); ++i)
{
QLabel *pLabel = new QLabel(this);
pLabel->setText(strList.at(i));
pLabel->setAlignment(Qt::AlignCenter);
list.append(pLabel);

pLabel->setObjectName("highlightLabel");
}

// 动画一
QPropertyAnimation *pAnimation1 = new QPropertyAnimation(list.at(0), "geometry");
pAnimation1->setDuration(1000);
pAnimation1->setStartValue(QRect(0, 0, 100, 30));
pAnimation1->setEndValue(QRect(120, 130, 100, 30));
pAnimation1->setEasingCurve(QEasingCurve::OutBounce);

// 暂停 - 特殊的动画
QPauseAnimation *pPauseAnimation = new QPauseAnimation(this);
pPauseAnimation->setDuration(1000);

// 动画二
QPropertyAnimation *pAnimation2 = new QPropertyAnimation(list.at(1), "geometry");
pAnimation2->setDuration(1000);
pAnimation2->setStartValue(QRect(120, 130, 120, 30));
pAnimation2->setEndValue(QRect(170, 0, 120, 30));
pAnimation2->setEasingCurve(QEasingCurve::OutInCirc);

m_pGroup = new QSequentialAnimationGroup(this);

// 添加动画
m_pGroup->addAnimation(pAnimation1);
m_pGroup->addAnimation(pPauseAnimation);
m_pGroup->addAnimation(pAnimation2);

// 循环2次
m_pGroup->setLoopCount(2);

// 连接信号槽
connect(pStartButton, SIGNAL(clicked(bool)), this, SLOT(startAnimation()));
connect(m_pGroup, SIGNAL(currentAnimationChanged(QAbstractAnimation*)), this, SLOT(onCurrentAnimationChanged(QAbstractAnimation*)));

...
}


槽函数如下:

// 开始动画
void MainWindow::startAnimation()
{
m_pGroup->start();
}

// 动画切换时会调用
void MainWindow::onCurrentAnimationChanged(QAbstractAnimation *current)
{
QPropertyAnimation *pAnimation = dynamic_cast<QPropertyAnimation *>(current);
if (pAnimation == NULL)
return;

QLabel *pLabel = dynamic_cast<QLabel *>(pAnimation->targetObject());
if (pLabel != NULL)
pLabel->setText(pLabel->text() + ".");
}


这里需要注意,暂停也是一个动画(比较特殊而已),所以我们需要用dynamic_cast来转换,并判断是否为NULL(否则会crash)。

更多参考

Qt之动画框架

Qt之QPropertyAnimation

Qt之QSequentialAnimationGroup

Qt之QParallelAnimationGroup

Qt之窗口动画(下坠、抖动、透明度)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: