zl程序教程

您现在的位置是:首页 >  工具

当前栏目

Qt之QPauseAnimation

Qt
2023-09-11 14:19:13 时间

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

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


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


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::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)。


QFuture 允许线程与一个或多个结果同步,这些结果将在稍后的时间点准备就绪,该结果可以是具有默认构造函数和拷贝构造函数的任何类型。如果一个结果在调用 result()、resultAt() 或 results() 函数时不可用,QFutur
每个网络接口可以包含零个或多个IP地址,进而可以关联到一个子网掩码和/或一个广播地址(取决于操作系统的支持)。 这个类代表一个这样的组。
在Qt编程过程中,通常会有多个部件嵌套,而大多数部件都有父子依赖关系,但是有些情况下不能直接引用子部件,这时我们可以通过父部件来findChild -“查找孩子”。
现代操作系统通常在桌面上提供一个特殊的区域,称为系统托盘或通知区域,长时间运行的应用程序可以显示图标和短消息。
大小策略会影响布局引擎处理部件的方式,部件加入布局以后,会返回一个QSizePolicy,描述了其水平和垂直方向的大小策略。可以通过QWidget::sizePolicy属性为特定部件设置大小策略。
QParallelAnimationGroup - 一个动画容器,当它启动的时候它里面的所有动画也启动,即:并行运行所有动画,当持续时间最长的动画完成时动画组也随之完成。
QPropertyAnimation以Qt属性做差值,作为属性值存储在QVariants中,该类继承自QVariantAnimation,并支持基类相同的元类型动画。 声明属性的类必须是一个QObject,为了能够让属性可以用做动画效果,必须提供一个setter(这样,QPropertyAnimatio