1. 程式人生 > 其它 >Qt QPropertyAnimation 幾行程式碼快速製作流暢的動畫效果

Qt QPropertyAnimation 幾行程式碼快速製作流暢的動畫效果

簡介

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 }