Qt实现图片旋转的几种方式(全)

您所在的位置:网站首页 图片旋转设置 Qt实现图片旋转的几种方式(全)

Qt实现图片旋转的几种方式(全)

2024-07-13 16:09| 来源: 网络整理| 查看: 265

目录

一、用手搓(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 #include #include #include #include #include ​ int main(int argc, char *argv[]) { QApplication app(argc, argv); ​ // 创建 QGraphicsScene QGraphicsScene scene; ​ // 加载图像到 QPixmap QPixmap 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); ​ // 显示 QGraphicsView view.show(); ​ // 创建定时器,并连接到槽函数以持续更新旋转角度 QTimer timer; QObject::connect(&timer, &QTimer::timeout, [&]() { static qreal rotationAngle = 0.0; rotationAngle += 1.0; // 每次增加旋转角度 ​ // 将旋转角度应用于 QGraphicsPixmapItem pixmapItem->setRotation(rotationAngle); }); timer.start(30); // 每 30 毫秒更新一次旋转角度 ​ return app.exec(); }

三、使用 QTransform 实现图像旋转

使用 Qt 提供的图形组件和定时器来实现图像的加载和旋转,通过设置合适的中心点以及应用 QTransform 进行变换操作,达到了让图像围绕自身中心点旋转的效果。

// 创建 QTransform 对象,并应用旋转变换 QTransform transform; transform.translate(pixmap.width() / 2, pi transform.rotate(rotationAngle); // 绕中心点旋转 transform.translate(-pixmap.width() / 2, - // 将变换应用于 QGraphicsPixmapItem pixmapItem->setTransform(transform);

通过使用 QGraphicsScene、QGraphicsView 和 QGraphicsPixmapItem 这些 Qt 图形组件来显示图像,并使用 QTimer 定时器来控制图像的旋转。

加载图像: 使用 QPixmap 加载图片,并将其放置在 QGraphicsPixmapItem 中,然后将该项添加到 QGraphicsScene 中,最终显示在 QGraphicsView 中。

设置中心点: 在 QGraphicsPixmapItem 中使用 setTransformOriginPoint() 将图像的中心点设置为图像的中心。

定时旋转: 创建 QTimer 定时器,每当定时器超时时,更新旋转角度。在超时槽函数中,使用 QTransform 进行变换处理。首先移动图像的原点到中心点,然后进行旋转,最后再将原点移回原位置。这样就实现了图像围绕自身中心点持续旋转的效果。

显示图像: 将 QGraphicsView 显示出来,图像会根据定时器的触发事件,在固定时间间隔内持续旋转显示。

#include #include #include #include #include #include #include int main(int argc, char *argv[]) { QApplication app(argc, argv); // 创建 QGraphicsScene QGraphicsScene scene; // 加载图像到 QPixmap QPixmap 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); // 显示 QGraphicsView view.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); // 将原点移回原位 // 将变换应用于 QGraphicsPixmapItem pixmapItem->setTransform(transform); }); timer.start(30); // 每 30 毫秒更新一次旋转角度 return app.exec(); } 四、利用 OpenGL

待研究



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3