Qt QWidget實現(xiàn)圖片旋轉(zhuǎn)動畫
更新時間:2024年12月28日 09:10:37 作者:小灰灰搞電子
這篇文章主要為大家詳細介紹了如何使用了Qt和QWidget實現(xiàn)圖片旋轉(zhuǎn)動畫效果,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
一、效果展示
二、源碼分享
本例程通過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)動畫的詳細內(nèi)容,更多關(guān)于Qt QWidget旋轉(zhuǎn)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
sublime text3搭建配置c語言編譯環(huán)境的詳細圖解教程(小白級)
這篇文章主要介紹了sublime text3搭建配置c語言編譯環(huán)境,詳細圖解,小白教程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-01-01深入C/C++浮點數(shù)在內(nèi)存中的存儲方式詳解
本篇文章是對C/C++浮點數(shù)在內(nèi)存中的存儲方式進行了詳細的分析介紹,需要的朋友參考下2013-05-05QT連接SQLServer數(shù)據(jù)庫的實現(xiàn)
要使用Qt連接SQL Server數(shù)據(jù)庫,需要使用Qt提供的SQL模塊和SQL Server驅(qū)動程序,具有一定的參考價值,感興趣的可以了解一下2023-09-09C++設(shè)計模式編程中的迭代器模式應(yīng)用解析
這篇文章主要介紹了C++設(shè)計模式編程中的迭代器模式應(yīng)用解析,迭代器模式注重對集合中元素的遍歷而不使其暴露,需要的朋友可以參考下2016-03-03