使用Qt實(shí)現(xiàn)旋轉(zhuǎn)動(dòng)畫(huà)效果
使用QPropertyAnimation類(lèi)綁定對(duì)應(yīng)的屬性后
就可以給這個(gè)屬性設(shè)置對(duì)應(yīng)的動(dòng)畫(huà)
//比如自定義了屬性 Q_PROPERTY(int rotation READ rotation WRITE setRotation) //給這個(gè)屬性加動(dòng)畫(huà)效果 //參數(shù)1:誰(shuí)要加動(dòng)畫(huà)效果 //參數(shù)2:哪個(gè)屬性加動(dòng)畫(huà)效果 //參數(shù)3:parent m_animation = new QPropertyAnimation(this, "rotation", this); m_animation -> setDuration(2000); //設(shè)置動(dòng)畫(huà)時(shí)長(zhǎng) m_animation -> setStartValue(0); //設(shè)置開(kāi)始值 m_animation -> setEndValue(360); //設(shè)置結(jié)束值 m_animation -> setLoopCount(3); //設(shè)置循環(huán)次數(shù) m_animation -> start(); //開(kāi)啟動(dòng)畫(huà)
動(dòng)畫(huà)開(kāi)啟后,就會(huì)不停的調(diào)用setRotation(屬性write函數(shù))去修改這個(gè)屬性的值
我們?cè)趕etRotation這個(gè)函數(shù)中修改屬性的值后,調(diào)用update()
于是QPropertyAnimation就會(huì)使得對(duì)應(yīng)的控件不停的重繪,就產(chǎn)生了動(dòng)畫(huà)效果。
舉例:
旋轉(zhuǎn)的矩形

完整代碼
#ifndef WIDGET_H
#define WIDGET_H
#include<QPropertyAnimation>
#include<QPainter>
#include <QWidget>
class RotatingWidget : public QWidget {
Q_OBJECT
//QPropertyAnimation類(lèi)要搭配Q_PROPERTY定義的屬性來(lái)使用
//本質(zhì)上就是QPropertyAnimation在不停的修改對(duì)應(yīng)屬性的值,然后不停的重繪,看起來(lái)像動(dòng)的效果
Q_PROPERTY(int rotation READ rotation WRITE setRotation)
public:
RotatingWidget(QWidget *parent = nullptr): QWidget(parent), m_rotation(0) {
m_animation = new QPropertyAnimation(this, "rotation", this);
m_animation->setDuration(2000);//設(shè)置動(dòng)畫(huà)時(shí)長(zhǎng)
m_animation->setStartValue(0);//設(shè)置開(kāi)始值
m_animation->setEndValue(360);//設(shè)置結(jié)束值
m_animation->setLoopCount(3);//設(shè)置循環(huán)次數(shù)
//還可以設(shè)置動(dòng)畫(huà)的效果曲線,是勻速還是先快后慢等
m_animation->start();//開(kāi)啟動(dòng)畫(huà)
}
int rotation() const {
return m_rotation;
}
public slots:
void setRotation(int angle) {
m_rotation = angle;
//屬性修改后就進(jìn)行重繪
update();
}
protected:
void paintEvent(QPaintEvent *event) override {
QWidget::paintEvent(event);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.translate(width() / 2, height() / 2);
painter.rotate(m_rotation);
painter.translate(-width() / 2, -height() / 2);
// 繪制旋轉(zhuǎn)的圖形,也可以是圖片
painter.setPen(QPen(Qt::red));
painter.drawRect(width() / 2-50, height() / 2-50, 100, 100);
}
private:
QPropertyAnimation *m_animation;
int m_rotation;
};
#endif // WIDGET_H到此這篇關(guān)于使用Qt實(shí)現(xiàn)旋轉(zhuǎn)動(dòng)畫(huà)效果的文章就介紹到這了,更多相關(guān)Qt旋轉(zhuǎn)動(dòng)畫(huà)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++?Protobuf實(shí)現(xiàn)接口參數(shù)自動(dòng)校驗(yàn)詳解
用C++做業(yè)務(wù)發(fā)開(kāi)的同學(xué)是否還在不厭其煩的編寫(xiě)大量if-else模塊來(lái)做接口參數(shù)校驗(yàn)?zāi)兀拷裉?,我們就模擬Java里面通過(guò)注解實(shí)現(xiàn)參數(shù)校驗(yàn)的方式來(lái)針對(duì)C++?protobuf接口實(shí)現(xiàn)一個(gè)更加方便、快捷的參數(shù)校驗(yàn)自動(dòng)工具,希望對(duì)大家有所幫助2023-04-04
C語(yǔ)言實(shí)現(xiàn)三子棋(井字棋)算法
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)三子棋(井字棋)算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
詳解C語(yǔ)言中telldir()函數(shù)和seekdir()函數(shù)的用法
這篇文章主要介紹了詳解C語(yǔ)言中telldir()函數(shù)和seekdir()函數(shù)的用法,是C語(yǔ)言入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09
C++實(shí)現(xiàn)和電腦對(duì)戰(zhàn)三子棋實(shí)例
大家好,本篇文章主要講的是C++實(shí)現(xiàn)和電腦對(duì)戰(zhàn)三子棋實(shí)例,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01
C++?list常用接口和模擬實(shí)現(xiàn)實(shí)例代碼
C++中l(wèi)ist容器底層實(shí)現(xiàn)是使用帶頭雙向循環(huán)鏈表的結(jié)構(gòu),通過(guò)指針指向前一個(gè)和后一個(gè)節(jié)點(diǎn),它也具有雙向鏈表的優(yōu)缺點(diǎn),下面給大家介紹C++?list常用接口和模擬實(shí)現(xiàn)代碼,感興趣的朋友一起看看吧2025-04-04
C語(yǔ)言 數(shù)據(jù)結(jié)構(gòu)與算法之字符串詳解
這篇文章將帶大家深入了解C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)與算法中的字符串,文中主要是介紹了字符串的定義、字符串的比較以及一些串的抽象數(shù)據(jù)類(lèi)型,感興趣的可以學(xué)習(xí)一下2022-01-01

