镇江网站建设教程,食品包装设计ppt模板,同济大学 土木工程毕业设计 久久建筑网,淄博网站快照优化公司目录
一、用手搓#xff08;QPainter#xff09;
二、使用 QGraphicsView 和 QGraphicsPixmapItem
三、使用 QTransform 实现图像旋转
四、利用 OpenGL 实现旋转图像的效果有几种不同的方法#xff0c;其中常见的包括#xff1a; 手动旋转绘制#xff1a; 使用 QPaint…
目录
一、用手搓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 QTimerint 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
待研究