簡介
QPropertyAnimation Class 是一個控制動畫效果的類,誕生自 Qt 4.6 版本。 該類繼承自 QVarianAnimation,並支持其它基類相同的動畫類,例如:QAnimationGroup 動畫組類,該類僅支持繼承自 QObject 類的窗口部件。
以例代勞
用例子來講述各個功能,直觀,立竿見影。
頭文件
1 #ifndef MAINWINDOW_H 2 #define MAINWINDOW_H
3 #include <QMainWindow>
4 namespace Ui { 5 class MainWindow; 6 } 7
8 class MainWindow : public QMainWindow 9 { 10 Q_OBJECT 11 public: 12 explicit MainWindow(QWidget *parent = 0); 13 ~MainWindow(); 14 private: 15 Ui::MainWindow *ui; 16 }; 17 #endif // MAINWINDOW_H
cpp文件
1 #include <QPropertyAnimation>
2 #include "mainwindow.h"
3 #include "ui_mainwindow.h"
4
5 MainWindow::MainWindow(QWidget *parent) : 6 QMainWindow(parent), 7 ui(new Ui::MainWindow) 8 { 9 ui->setupUi(this); 10
11 /* 聲明動畫類,並將控制對象 this (this一定是繼承自QObject的窗口部件) 以及屬性名 "geometry" 傳入構造函數 */
12 QPropertyAnimation* animation = new QPropertyAnimation(this, "geometry"); 13 /* 設置動畫持續時長為 2 秒鍾 */
14 animation->setDuration(2000); 15 /* 設置動畫的起始狀態 起始點 (1,2) 起始大小 (3,4) */
16 animation->setStartValue(QRect(1, 2, 3, 4)); 17 /* 設置動畫的結束狀態 結束點 (100,200) 結束大小 (300,400) */
18 animation->setEndValue(QRect(100, 200, 300, 400)); 19 /* 設置動畫效果 */
20 animation->setEasingCurve(QEasingCurve::OutInExpo); 21 /* 開始執行動畫 QAbstractAnimation::DeleteWhenStopped 動畫結束后進行自清理(效果就好像智能指針里的自動delete animation) */
22 animation->start(QAbstractAnimation::DeleteWhenStopped); 23 } 24
25 MainWindow::~MainWindow() 26 { 27 delete ui; 28 }