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 }  

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM