简述

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之QPauseAnimation的更多相关文章

  1. Qt之窗口动画(下坠、抖动、透明度)

    简述 前面几节中我们介绍了关于动画的基本使用,有属性动画.串行动画组.并行动画组.这节我们来实现一些特效,让交互更顺畅. 简述 示例 效果 源码 更多参考 示例 下面,我们以geometry.pos. ...

  2. Qt之QParallelAnimationGroup

    简述 QParallelAnimationGroup类提供动画的并行组. QParallelAnimationGroup - 一个动画容器,当它启动的时候它里面的所有动画也启动,即:并行运行所有动画, ...

  3. Qt之QSequentialAnimationGroup

    简述 QSequentialAnimationGroup类提供动画的串行组. QSequentialAnimationGroup是一个串行运行动画的QAnimationGroup,在另一个动画播放结束 ...

  4. Qt之QPropertyAnimation

    简述 QPropertyAnimation类定义了Qt的属性动画. QPropertyAnimation以Qt属性做差值,作为属性值存储在QVariants中,该类继承自QVariantAnimati ...

  5. Qt之动画框架

    简述 Qt动画框架旨在为创建动画和平滑的GUI提供了一种简单的方法.通过Qt动画属性,该框架为部件和其它QObject对象的动画操作提供了非常大的自由性,框架也可以被用于图形视图框架中,动画框架中许多 ...

  6. 《Qt 实战一二三》

    简介 "我们来自Qt分享&&交流,我们来自Qt Quick分享&&交流",不管你是笑了,还是笑了,反正我们是认真的.我们就是要找寻一种Hold不住的 ...

  7. Qt之窗口动画(下坠、抖动、透明度)(还有好多相关帖子)

    简述 前面几节中我们介绍了关于动画的基本使用,有属性动画.串行动画组.并行动画组.这节我们来实现一些特效,让交互更顺畅. 简述 示例 效果 源码 更多参考 示例 下面,我们以geometry.pos. ...

  8. Qt动画效果的实现,QPropertyAnimation

    Qt动画架构中的主要类如下图所示: 动画框架由基类QAbstractAnimation和它的两个儿子QVariantAnimation和QAnimationGroup组成.QAbstractAnima ...

  9. Qt动画与Qt坐标小记

    Qt动画 转载自: <http://jingyan.baidu.com/article/154b46315757b628ca8f4116.html> 和  <http://blog. ...

随机推荐

  1. MVC开发模式

     关于mvc详情可参阅:http://www.w3school.com.cn/aspnet/mvc_intro.asp MVC (Modal View Controler)本来是存在于Desktop程 ...

  2. Threads in Spring

    使用Spring时经常会问,我们定义的Bean应该是Singleton还是Prototype?多个客户端同时调用Dao层,需要考虑线程安全吗?通过阅读官方文档和Spring的源代码,这类问题的答案是: ...

  3. Creating HTML table with vertically oriented text as table header 表头文字方向

    AS an old question, this is more like info or reminder about vertical margin or padding in % that ta ...

  4. Python中时间的处理之——timedelta篇

      #! /usr/bin/python # coding=utf-8 from datetime import datetime,timedelta """ timed ...

  5. c#扩展方法的理解(二:接口)

    namespace ExtensionInterfaceMethod { class Program { static void Main(string[] args) { //使用接口变量来调用扩展 ...

  6. 字符串表达式String Expressions

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  7. FZU 2212 Super Mobile Charger(超级充电宝)

    [Description] [题目描述] While HIT ACM Group finished their contest in Shanghai and is heading back Harb ...

  8. python介绍(转载)

    Python简介 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言 ...

  9. iOS常用define宏定义

    1. 屏幕宽高及常用尺寸 #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)#define SCREEN_HEIGHT ([U ...

  10. poj2986A Triangle and a Circle&&poj3675Telescope(三角形剖分)

    链接 2986是3675的简化版,只有一个三角形. 这题主要在于求剖分后三角形与圆的相交面积,需要分情况讨论. 具体可以看此博客 http://hi.baidu.com/billdu/item/703 ...