目录

一、用手搓(QPainter)

二、使用 QGraphicsView 和 QGraphicsPixmapItem

三、使用 QTransform 实现图像旋转

四、利用 OpenGL


实现旋转图像效果几种不同的方法,其中常见的包括:

  1. 手动旋转绘制 使用 QPainter 的旋转函数,手动计算旋转后的坐标绘制图像。这种方式需要自己处理旋转后的坐标变换,相对复杂一些。

  2. 使用 QGraphicsView 和 QGraphicsPixmapItem 使用 QGraphicsView 架构绘制图形对象,并在 QGraphicsScene 中添加 QGraphicsPixmapItem,然后通过旋转 QGraphicsPixmapItem 实现图像旋转。

  3. 使用 QTransform 使用 QTransform 类来应用变换,通过旋转矩阵来对图像进行旋转,然后使用 QPainter 绘制旋转后的图像。这种方法能够简化坐标变换的操作

  4. 利用 OpenGL: 利用 Qt 的 QOpenGLWidget 和 OpenGL 的旋转操作,在 OpenGL 上下文中进行图像的绘制和旋转。这种方法适用于需要更高级别的图形操作性能要求较高的场景

每种方法都有其特点和适用场景选择其中一种取决于你的需求和熟悉程度。通常情况下,QTransform 是实现图像旋转最常用且较为简便的方式。

一、用手搓(QPainter

  1. 创建绘图设备(QPainter): 首先,需要创建一个 QPainter 对象用于执行绘图操作

  2. 设置绘图参数: 在进行绘图之前,可以通过 QPainter 的函数设置渲染参数,如反锯齿等。

  3. 加载图像: 使用 QPixmap 加载需要绘制的图像。

  4. 设置绘图变换: 使用 QPainter 的变换函数(例如 translaterotate 等),将绘图坐标系转换到图像的中心,并按需求进行旋转。

  5. 绘制图像: 使用 QPainter 的 drawPixmap 函数,在指定位置绘制图像。

  6. 重置绘图变换(可选): 如果在绘制其他内容之前需要恢复坐标系变换,则可以使用 QPainter 的 resetTransform 函数重置坐标系

  7. 完成绘图绘图完成后,程序自动将绘图设备内容显示窗口上,或者在需要时手动调用 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()数中参数,可以设置图像的位置和旋转角度。这种方法相比手动绘制更简单,并且使用 QGraphicsViewQGraphicsPixmapItem 更方便地进行图像的操作显示

7、创建一个定时器 QTimer,并将其连接到一个 Lambda 函数,Lambda 函数中每次定时器超时时都会更新图像的旋转角度。在每个超时事件中,图像的旋转角度增加 1 度,并通过 setRotation() 函数应用QGraphicsPixmapItem,从而使图像持续旋转。然后使用 timer.start() 启动定时器,并设定每 30 毫秒更新一次旋转角度

#include <QApplication&gt;
#include <QGraphicsScene&gt;
#include <QGraphicsView&gt;
#include <QGraphicsPixmapItem&gt;
#include <QPixmap&gt;
#include <QTimer>
​
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(&amp;scene);
​
    // 显示 QGraphicsView
    view.show();
​
    // 创建定时器,并连接到槽函数以持续更新旋转角度
    QTimer timer;
    QObject::connect(&amp;timer, &amp;QTimer::timeout, [&amp;]() {
        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);

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

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

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

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

  4. 显示图像: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);

    // 创建 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(&amp;scene);

    // 显示 QGraphicsView
    view.show();

    // 创建定时器,并连接到槽函数以持续更新旋转角度
    QTimer timer;
    QObject::connect(&amp;timer, &amp;QTimer::timeout, [&amp;]() {
        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

研究

原文地址:https://blog.csdn.net/qq_43445867/article/details/134486077

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_35560.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注