一杯清酒邀明月
天下本无事,庸人扰之而烦耳。

简述

QParallelAnimationGroup类提供动画的并行组。

QParallelAnimationGroup - 一个动画容器,当它启动的时候它里面的所有动画也启动,即:并行运行所有动画,当持续时间最长的动画完成时动画组也随之完成。

详细描述

QParallelAnimationGroup可以被当做任何其它的QAbstractAnimation动画,例如:暂停、重置、添加到其它动画组中。

1 QParallelAnimationGroup *group = new QParallelAnimationGroup;
2 group->addAnimation(anim1);
3 group->addAnimation(anim2);
4 group->start();

这个例子中,anim1、anim2是QPropertyAnimation。

示例

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

效果

源码

 1 MainWindow::MainWindow(QWidget *parent)
 2     : CustomWindow(parent)
 3 {
 4     ...
 5 
 6     QPushButton *pStartButton = new QPushButton(this);
 7     pStartButton->setText(QString::fromLocal8Bit("开始动画"));
 8 
 9     QList<QLabel *> list;
10     QStringList strList;
11     strList << QString::fromLocal8Bit("一去丶二三里") << QString::fromLocal8Bit("青春不老,奋斗不止");
12 
13     for (int i = 0; i < strList.count(); ++i)
14     {
15         QLabel *pLabel = new QLabel(this);
16         pLabel->setText(strList.at(i));
17         pLabel->setAlignment(Qt::AlignCenter);
18         pLabel->setStyleSheet("color: rgb(0, 160, 230);");
19         list.append(pLabel);
20     }
21 
22     // 动画一
23     QPropertyAnimation *pAnimation1 = new QPropertyAnimation(list.at(0), "geometry");
24     pAnimation1->setDuration(1000);
25     pAnimation1->setStartValue(QRect(0, 0, 100, 30));
26     pAnimation1->setEndValue(QRect(120, 130, 100, 30));
27     pAnimation1->setEasingCurve(QEasingCurve::OutBounce);
28 
29     // 动画二
30     QPropertyAnimation *pAnimation2 = new QPropertyAnimation(list.at(1), "geometry");
31     pAnimation2->setDuration(1000);
32     pAnimation2->setStartValue(QRect(120, 130, 120, 30));
33     pAnimation2->setEndValue(QRect(170, 0, 120, 30));
34     pAnimation2->setEasingCurve(QEasingCurve::OutInCirc);
35 
36     m_pGroup = new QParallelAnimationGroup(this);
37 
38     // 添加动画
39     m_pGroup->addAnimation(pAnimation1);
40     m_pGroup->addAnimation(pAnimation2);
41 
42     // 循环2次
43     m_pGroup->setLoopCount(2);
44 
45     connect(pStartButton, SIGNAL(clicked(bool)), this, SLOT(startAnimation()));
46 
47     ...
48 }
49 
50 // 开始动画
51 void MainWindow::startAnimation()
52 {
53     m_pGroup->start();
54 }

 

posted on 2021-06-24 13:34  一杯清酒邀明月  阅读(505)  评论(0编辑  收藏  举报