Qt QWidget實現(xiàn)圖片旋轉(zhuǎn)動畫
一、效果展示

二、源碼分享
本例程通過QGraphicsView實現(xiàn)svg格式圖片旋轉(zhuǎn)。

.hpp
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QGraphicsSvgItem>
#include <QGraphicsScene>
#include <QTimer>
#include <QPropertyAnimation>
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
QGraphicsSvgItem *graphItem;
QGraphicsScene *graphScene;
};
#endif // MAINWINDOW_H
.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->graphScene = new QGraphicsScene();
this->ui->graphicsView->setScene(this->graphScene);
this->graphItem = new QGraphicsSvgItem( ":/image/running.svg" );
this->graphItem->setScale(0.5);
QRectF boundingRect = this->graphItem->boundingRect();
this->graphItem->setTransformOriginPoint(boundingRect.width() / 2, boundingRect.height() / 2);
graphScene->addItem( this->graphItem );
this->graphItem->setRotation(45);
// 創(chuàng)建一個QPropertyAnimation對象來控制旋轉(zhuǎn)屬性
QPropertyAnimation* rotationAnimation = new QPropertyAnimation(this->graphItem, "rotation");
// 設(shè)置動畫的起始值和結(jié)束值
rotationAnimation->setStartValue(0);
rotationAnimation->setEndValue(360);
// 設(shè)置動畫持續(xù)時間(以毫秒為單位)
rotationAnimation->setDuration(3000);
// 設(shè)置動畫循環(huán)次數(shù)(-1表示無限循環(huán))
rotationAnimation->setLoopCount(-1);
// 啟動動畫
rotationAnimation->start();
this->ui->graphicsView->installEventFilter(this);
this->ui->graphicsView->centerOn(this->graphItem);
}
MainWindow::~MainWindow()
{
delete ui;
}
以上就是Qt QWidget實現(xiàn)圖片旋轉(zhuǎn)動畫的詳細(xì)內(nèi)容,更多關(guān)于Qt QWidget旋轉(zhuǎn)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C++利用對象池優(yōu)化內(nèi)存管理解決MISRA報警的代碼詳解
本篇詳細(xì)講解如何用對象池技術(shù)優(yōu)化C++項目中的內(nèi)存管理,徹底消除new/delete帶來的MISRA報警,兼顧高性能與安全規(guī)范,需要的朋友可以參考下2025-07-07
C++數(shù)據(jù)結(jié)構(gòu)之哈希表的實現(xiàn)
哈希表,即散列表,可以快速地存儲和查詢記錄。這篇文章主要為大家詳細(xì)介紹了C++數(shù)據(jù)結(jié)構(gòu)中哈希表的實現(xiàn),感興趣的小伙伴可以了解一下2023-03-03
C語言數(shù)據(jù)結(jié)構(gòu)與算法之排序總結(jié)(一)
這篇文章主要介紹了數(shù)據(jù)結(jié)構(gòu)與算法中的插入類和交換類的各種排序,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2021-12-12
探討:將兩個鏈表非降序合并為一個鏈表并依然有序的實現(xiàn)方法
本篇文章是對將兩個鏈表非降序合并為一個鏈表并依然有序的實現(xiàn)方法進行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
c++中為什么可以通過指針或引用實現(xiàn)多態(tài)詳解
這篇文章主要給大家介紹了關(guān)于c++中為何可以通過指針或引用實現(xiàn)多態(tài),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
C++任意進制轉(zhuǎn)換的代碼實現(xiàn)與優(yōu)化技巧
在編程中,進制轉(zhuǎn)換是一個非常常見的操作,我們常常需要將一個數(shù)從一種進制轉(zhuǎn)換為另一種進制,在本文中,我們將探討如何使用 C++ 實現(xiàn)從任意進制到任意進制的轉(zhuǎn)換,并對代碼進行優(yōu)化,使其更加高效和可讀,需要的朋友可以參考下2025-07-07

