1. 程式人生 > 其它 >Qt QParallelAnimationGroup 並行動畫組

Qt QParallelAnimationGroup 並行動畫組

簡述

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 }