目錄
一、用手搓(QPainter)
二、使用 QGraphicsView 和 QGraphicsPixmapItem
三、使用 QTransform 實現圖像旋轉
四、利用 OpenGL
實現旋轉圖像的效果有幾種不同的方法,其中常見的包括:
-
手動旋轉繪制: 使用 QPainter 的旋轉函數,手動計算旋轉后的坐標并繪制圖像。這種方式需要自己處理旋轉后的坐標變換,相對復雜一些。
-
使用 QGraphicsView 和 QGraphicsPixmapItem: 使用 QGraphicsView 架構繪制圖形對象,并在 QGraphicsScene 中添加 QGraphicsPixmapItem,然后通過旋轉 QGraphicsPixmapItem 實現圖像旋轉。
-
使用 QTransform: 使用 QTransform 類來應用變換,通過旋轉矩陣來對圖像進行旋轉,然后使用 QPainter 繪制旋轉后的圖像。這種方法能夠簡化坐標變換的操作。
-
利用 OpenGL: 利用 Qt 的 QOpenGLWidget 和 OpenGL 的旋轉操作,在 OpenGL 上下文中進行圖像的繪制和旋轉。這種方法適用于需要更高級別的圖形操作和性能要求較高的場景。
每種方法都有其特點和適用場景,選擇其中一種取決于你的需求和熟悉程度。通常情況下,QTransform 是實現圖像旋轉最常用且較為簡便的方式。
一、用手搓(QPainter)
創建繪圖設備(QPainter): 首先,需要創建一個 QPainter 對象,用于執行繪圖操作。
設置繪圖參數: 在進行繪圖之前,可以通過 QPainter 的函數設置渲染參數,如反鋸齒等。
加載圖像: 使用 QPixmap 加載需要繪制的圖像。
設置繪圖變換: 使用 QPainter 的變換函數(例如 translate、rotate 等),將繪圖坐標系轉換到圖像的中心,并按需求進行旋轉。
繪制圖像: 使用 QPainter 的 drawPixmap 函數,在指定的位置繪制圖像。
重置繪圖變換(可選): 如果在繪制其他內容之前需要恢復坐標系變換,則可以使用 QPainter 的 resetTransform 函數重置坐標系。
完成繪圖: 繪圖完成后,程序會自動將繪圖設備的內容顯示到窗口上,或者在需要時手動調用 update 函數觸發窗口的重繪。
?手搓代碼:https://download.csdn.net/download/qq_43445867/88562187
二、使用 QGraphicsView 和 QGraphicsPixmapItem
1、創建一個 QGraphicsScene
對象。然后加載圖像到 QPixmap
中,
2、創建?QGraphicsPixmapItem
對象,并將圖像加載到這個 QGraphicsPixmapItem
中。
3、分別使用?setPos()
和 setRotation()
函數,設置圖像的位置和旋轉角度。
4、將 QGraphicsPixmapItem
添加到 QGraphicsScene
中
5、創建一個 QGraphicsView
對象,并將 QGraphicsScene
設置為 QGraphicsView
的場景,最終顯示了這個 QGraphicsView
。
6、通過修改 setPos()
和 setRotation()
函數中的參數,可以設置圖像的位置和旋轉角度。這種方法相比手動繪制更簡單,并且使用 QGraphicsView
和 QGraphicsPixmapItem
更方便地進行圖像的操作和顯示。
7、創建一個定時器 QTimer
,并將其連接到一個 Lambda 函數,Lambda 函數中每次定時器超時時都會更新圖像的旋轉角度。在每個超時事件中,圖像的旋轉角度增加 1 度,并通過 setRotation()
函數應用于 QGraphicsPixmapItem
,從而使圖像持續旋轉。然后使用 timer.start()
啟動定時器,并設定每 30 毫秒更新一次旋轉角度
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsPixmapItem>
#include <QPixmap>
#include <QTimer>
?
int main(int argc, char *argv[]) {QApplication app(argc, argv);
?// 創建 QGraphicsSceneQGraphicsScene scene;
?// 加載圖像到 QPixmapQPixmap pixmap("statI.png");
?// 創建 QGraphicsPixmapItem 并將圖像加載到 QGraphicsPixmapItem 中QGraphicsPixmapItem *pixmapItem = new QGraphicsPixmapItem(pixmap);
?// 設置圖像的旋轉中心點為圖像中心pixmapItem->setTransformOriginPoint(pixmap.width() / 2, pixmap.height() / 2);
?// 將 QGraphicsPixmapItem 添加到 QGraphicsScene 中scene.addItem(pixmapItem);
?// 創建 QGraphicsView,并將 QGraphicsScene 設置為它的場景QGraphicsView view(&scene);
?// 顯示 QGraphicsViewview.show();
?// 創建定時器,并連接到槽函數以持續更新旋轉角度QTimer timer;QObject::connect(&timer, &QTimer::timeout, [&]() {static qreal rotationAngle = 0.0;rotationAngle += 1.0; // 每次增加旋轉角度
?// 將旋轉角度應用于 QGraphicsPixmapItempixmapItem->setRotation(rotationAngle);});timer.start(30); // 每 30 毫秒更新一次旋轉角度
?return app.exec();
}
三、使用 QTransform
實現圖像旋轉
使用?Qt 提供的圖形組件和定時器來實現圖像的加載和旋轉,通過設置合適的中心點以及應用 QTransform
進行變換操作,達到了讓圖像圍繞自身中心點旋轉的效果。
// 創建 QTransform 對象,并應用旋轉變換QTransform transform;transform.translate(pixmap.width() / 2, pitransform.rotate(rotationAngle); // 繞中心點旋轉transform.translate(-pixmap.width() / 2, -// 將變換應用于 QGraphicsPixmapItempixmapItem->setTransform(transform);
通過使用 QGraphicsScene
、QGraphicsView
和 QGraphicsPixmapItem
這些 Qt 圖形組件來顯示圖像,并使用 QTimer
定時器來控制圖像的旋轉。
加載圖像: 使用
QPixmap
加載圖片,并將其放置在QGraphicsPixmapItem
中,然后將該項添加到QGraphicsScene
中,最終顯示在QGraphicsView
中。設置中心點: 在
QGraphicsPixmapItem
中使用setTransformOriginPoint()
將圖像的中心點設置為圖像的中心。定時旋轉: 創建
QTimer
定時器,每當定時器超時時,更新旋轉角度。在超時槽函數中,使用QTransform
進行變換處理。首先移動圖像的原點到中心點,然后進行旋轉,最后再將原點移回原位置。這樣就實現了圖像圍繞自身中心點持續旋轉的效果。顯示圖像: 將
QGraphicsView
顯示出來,圖像會根據定時器的觸發事件,在固定時間間隔內持續旋轉顯示。
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsPixmapItem>
#include <QPixmap>
#include <QTransform>
#include <QTimer>int main(int argc, char *argv[]) {QApplication app(argc, argv);// 創建 QGraphicsSceneQGraphicsScene scene;// 加載圖像到 QPixmapQPixmap pixmap(":/path/to/your/image.png");// 創建 QGraphicsPixmapItem 并將圖像加載到 QGraphicsPixmapItem 中QGraphicsPixmapItem *pixmapItem = new QGraphicsPixmapItem(pixmap);// 設置圖像的中心點為原點pixmapItem->setTransformOriginPoint(pixmap.width() / 2, pixmap.height() / 2);// 將 QGraphicsPixmapItem 添加到 QGraphicsScene 中scene.addItem(pixmapItem);// 創建 QGraphicsView,并將 QGraphicsScene 設置為它的場景QGraphicsView view(&scene);// 顯示 QGraphicsViewview.show();// 創建定時器,并連接到槽函數以持續更新旋轉角度QTimer timer;QObject::connect(&timer, &QTimer::timeout, [&]() {static qreal rotationAngle = 0.0;rotationAngle += 1.0; // 每次增加旋轉角度// 創建 QTransform 對象,并應用旋轉變換QTransform transform;transform.translate(pixmap.width() / 2, pixmap.height() / 2); // 將原點移動到中心transform.rotate(rotationAngle); // 繞中心點旋轉transform.translate(-pixmap.width() / 2, -pixmap.height() / 2); // 將原點移回原位// 將變換應用于 QGraphicsPixmapItempixmapItem->setTransform(transform);});timer.start(30); // 每 30 毫秒更新一次旋轉角度return app.exec();
}
四、利用 OpenGL
待研究