簡介
QPropertyAnimation Class 是一個控制動畫效果的類,誕生自 Qt 4.6 版本。 該類繼承自 QVarianAnimation,並支持其它基類相同的動畫類,例如:QAnimationGroup 動畫組類,該類僅支持繼承自 QObject 類的窗口部件。
以例代勞
用例子來講述各個功能,直觀,立竿見影。
頭文件
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
- #include <QMainWindow>
- namespace Ui {
- class MainWindow;
- }
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
- public:
- explicit MainWindow(QWidget *parent = 0);
- ~MainWindow();
- private:
- Ui::MainWindow *ui;
- };
- #endif // MAINWINDOW_H
cpp文件
- #include <QPropertyAnimation>
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- MainWindow::MainWindow(QWidget *parent) :
- QMainWindow(parent),
- ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- /* 聲明動畫類,並將控制對象 this (this一定是繼承自QObject的窗口部件) 以及屬性名 "geometry" 傳入構造函數 */
- QPropertyAnimation* animation = new QPropertyAnimation(this, "geometry");
- /* 設置動畫持續時長為 2 秒鍾 */
- animation->setDuration(2000);
- /* 設置動畫的起始狀態 起始點 (1,2) 起始大小 (3,4) */
- animation->setStartValue(QRect(1, 2, 3, 4));
- /* 設置動畫的結束狀態 結束點 (100,200) 結束大小 (300,400) */
- animation->setEndValue(QRect(100, 200, 300, 400));
- /* 設置動畫效果 */
- animation->setEasingCurve(QEasingCurve::OutInExpo);
- /* 開始執行動畫 QAbstractAnimation::DeleteWhenStopped 動畫結束后進行自清理(效果就好像智能指針里的自動delete animation) */
- animation->start(QAbstractAnimation::DeleteWhenStopped);
- }
- MainWindow::~MainWindow()
- {
- delete ui;
- }
QPropertyAnimation 聲明的時候
可以傳入的屬性分別有 pos(位置)、windowOpacity(透明度)
位置示例
- void OEasyWebNotice::onShow() {
- QRect rect = QApplication::desktop()->availableGeometry();
- const int &endy = rect.height() - height();
- QPropertyAnimation *animation= new QPropertyAnimation(this,"pos");
- animation->setDuration(2000);
- animation->setStartValue(QPoint(rect.width() - width(), rect.height()));
- animation->setEndValue(QPoint(rect.width() - width(), endy));
- animation->setEasingCurve(QEasingCurve::OutCubic);
- connect(animation, SIGNAL(finished()),
- this, SLOT(animationFinished()));
- show();
- animation->start(QAbstractAnimation::DeleteWhenStopped);
- }
透明度示例
- void OEasyWebNotice::onClose(void) {
- disconnect(closeButton_.get(),SIGNAL(clicked()),
- this, SLOT(onClose()));
- QPropertyAnimation* animation = new QPropertyAnimation(this, "windowOpacity");
- animation->setDuration(1000);
- animation->setStartValue(1);
- animation->setEndValue(0);
- animation->setEasingCurve(QEasingCurve::InCirc);
- connect(animation, SIGNAL(finished()),
- this, SLOT(deleteLater()));
- show();
- animation->start(QAbstractAnimation::DeleteWhenStopped);
- }
