Qt 自定义动画属性 QPropertyAnimation
簡述
QPropertyAnimation類定義了Qt的屬性動畫。
QPropertyAnimation以Qt屬性做差值,作為屬性值存儲在QVariants中,該類繼承自QVariantAnimation,并支持基類相同的元類型動畫。
聲明屬性的類必須是一個QObject,為了能夠讓屬性可以用做動畫效果,必須提供一個setter(這樣,QPropertyAnimation才可以設置屬性的值)。注意:這能夠使它讓許多Qt控件產生動畫效果。
?
- 簡述
- 詳細描述
- 公共函數
- 示例
- 原始屬性
- 效果
- 源碼
- 自定義屬性
- 效果
- 源碼
- 原始屬性
- 更多參考
?
詳細描述
來看一個示例:
QPropertyAnimation *animation = new QPropertyAnimation(myWidget, "geometry"); animation->setDuration(10000); animation->setStartValue(QRect(0, 0, 100, 30)); animation->setEndValue(QRect(250, 250, 100, 30));animation->start();首先,我們通過構造函數創建一個QPropertyAnimation對象,其中myWidget表示動畫作用的QObject對象,geometry則表示QObject的屬性。然后,可以指定屬性的開始值和結束值。此過程等于在你自己的類中實現了自定義屬性 - 需要用QVariantAnimation檢測你自定義的QVariant類型是否支持。
QVariantAnimation類詳細的描述了如何設置動畫。需要注意的是:如果沒有設置起始值,在QPropertyAnimation實例被創建時,屬性就會設置起始值為它有的值。
QPropertyAnimation就其本身而言非常奏效。對于復雜的動畫,例如:包含多個對象,則可以使用QAnimationGroup,動畫組是一個可以包含其它動畫的動畫,并可以管理動畫的播放。可以參考QParallelAnimationGroup的示例。
公共函數
-
QByteArray propertyName() const
返回動畫的目標屬性名 -
void setPropertyName(const QByteArray & propertyName)
設置動畫的目標屬性名 -
void setTargetObject(QObject * target)
設置動畫作用的QObject對象 -
QObject * targetObject() const
返回動畫作用的QObject對象
示例
原始屬性
下面,利用上面講解的geometry屬性,來實現一個動畫坐標變化。
效果
源碼
QPropertyAnimation *pAnimation = new QPropertyAnimation(m_pLabel, "geometry"); pAnimation->setDuration(1000); pAnimation->setStartValue(QRect(0, 0, 75, 25)); pAnimation->setEndValue(QRect(200, 130, 75, 25)); pAnimation->setEasingCurve(QEasingCurve::OutBounce); // 緩和曲線風格 connect(pStartButton, SIGNAL(clicked(bool)), pAnimation, SLOT(start()));自定義屬性
通過自定義屬性alpha,來使用動畫設置標簽的樣式。
效果
源碼
#ifndef MAIN_WINDOW_H #define MAIN_WINDOW_H...class MainWindow : public CustomWindow {Q_OBJECTQ_PROPERTY(int alpha READ alpha WRITE setAlpha)public:explicit MainWindow(QWidget *parent = 0);~MainWindow();private:int alpha() const;void setAlpha(const int alpha);private:int m_nAlpha;QLabel *m_pLabel; };#endif // MAIN_WINDOW_H #include "main_window.h"MainWindow::MainWindow(QWidget *parent): CustomWindow(parent) {...QPushButton *pStartButton = new QPushButton(this);pStartButton->setText(QString::fromLocal8Bit("開始動畫"));m_pLabel = new QLabel(this);m_pLabel->setText(QString::fromLocal8Bit("一去丶二三里"));m_pLabel->setAlignment(Qt::AlignCenter);m_pLabel->setStyleSheet("color: rgb(0, 160, 230);");QPropertyAnimation *pAnimation = new QPropertyAnimation();pAnimation->setTargetObject(this);pAnimation->setPropertyName("alpha");pAnimation->setDuration(1000);pAnimation->setKeyValueAt(0, 255);pAnimation->setKeyValueAt(0.5, 100);pAnimation->setKeyValueAt(1, 255);pAnimation->setLoopCount(-1); //永遠運行,直到stopconnect(pStartButton, SIGNAL(clicked(bool)), pAnimation, SLOT(start()));... }int MainWindow::alpha() const {return m_nAlpha; }void MainWindow::setAlpha(const int alpha) {m_nAlpha = alpha;QString strQSS = QString("color: rgb(0, 160, 230); background-color: rgba(10, 160, 105, %1);").arg(m_nAlpha);m_pLabel->setStyleSheet(strQSS); }O(∩_∩)O~是不是很easy,如果你想要實現更多其它效果,都可以自定義。但一定要注意以下兩點:
更多參考
- Qt之動畫框架
- Qt之QSequentialAnimationGroup
- Qt之QParallelAnimationGroup
- Qt之QPauseAnimation
- Qt之窗口動畫(下墜、抖動、透明度)
總結
以上是生活随笔為你收集整理的Qt 自定义动画属性 QPropertyAnimation的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用正则表达式匹配网址URL中最后一个反斜
- 下一篇: 所有程序自适应高分辨率(未完善)