Qt之QPauseAnimation
2024-08-25 17:43:00
简述
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)。
更多参考
最新文章
- 关于settimeout 和for循环
- 设置随处都可以调用tcc的编译环境
- JQuery 回到顶部效果
- 你是怎么理解“MVC”的
- asp.net EasyUI DataGrid 实现增删改查
- TatukGIS - GisDefs - CreateMSJET 函数
- centos U盘安装
- SpringMVC 异常的处理
- 常用Markdown公式整理 &;&; 页内跳转注意 &;&; Markdown preview
- Uncaught TypeError: Cannot read property &#39;getters&#39; of undefined
- 内置函数_range()
- CoordinatorLayout实现的效果(标题栏效果)
- 什么是内联函数(inline function)
- python 时间元组转时间戳
- C++的开源跨平台日志库glog学习研究(一)
- 一些centos 6和centos 7的区别
- 使用 Dojo 掌握面向对象开发
- sublime text2下配置c++
- 【OneNote】使用线性格式输入数学公式
- WampServer常用环境配置
热门文章
- 【leetcode❤python】 290. Word Pattern
- BZOJ 3672 [Noi2014]购票 (熟练剖分+凸壳维护)
- HDU 3213 Box Relations(拓扑排序构造)
- [UVa1210]Sum of Consecutive Prime Numbers(前缀和,打表)
- CSS定位小技巧
- Android 测试工具
- 关于gitlab的一个问题解决
- [转载] [Mark]分布式存储必读论文
- Promise A 规范的一个简单的浏览器端实现
- maven pom.xml解释 (转)