qt QGraphicsRotation详解

1、概述

QGraphicsRotation 是 Qt 框架中 QGraphicsTransform 的一个子类,它专门用于处理图形项的旋转变换。通过 QGraphicsRotation,你可以对 QGraphicsItem(如形状、图片等)进行旋转操作,从而创建动态和吸引人的视觉效果。QGraphicsRotation 提供了对旋转角度、旋转中心以及是否应用变换的精细控制。

2、重要方法

QGraphicsRotation 类提供了一系列方法来设置和获取旋转属性:

  • angle()setAngle(qreal angle):用于获取和设置旋转的角度(以度为单位)。
  • origin()setOrigin(const QPointF &origin):用于获取和设置旋转的中心点。
  • applyTo(QTransform &transform) const :将旋转变换应用到给定的 QTransform 对象上。
  • update():当旋转属性发生变化时,调用此方法以更新变换。

此外,QGraphicsRotation 还继承了一些来自 QGraphicsTransform 的方法,如 itemTransform(const QGraphicsItem *item) const,它返回应用于给定图形项的变换。

复制代码
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsRotation>
#include <QPropertyAnimation>
#include <QGraphicsRectItem>
#include <QVector3D>

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    // 创建场景和视图
    QGraphicsScene scene;
    QGraphicsView view(&scene);

    // 创建一个矩形项
    QGraphicsRectItem *rect = new QGraphicsRectItem(0, 0, 100, 100);
    rect->setBrush(Qt::blue);
    scene.addItem(rect);

    // 创建旋转变换
    QGraphicsRotation *rotation = new QGraphicsRotation;
    rotation->setOrigin(QVector3D(50, 50, 0)); // 设置旋转中心点为矩形中心
    rect->setTransformations(QList<QGraphicsTransform*>() << rotation);

    // 创建动画
    QPropertyAnimation *animation = new QPropertyAnimation(rotation, "angle");
    animation->setDuration(2000); // 2秒
    animation->setStartValue(0);
    animation->setEndValue(360);
    animation->setLoopCount(-1); // 无限循环

    // 显示视图并启动动画
    view.resize(300, 300);
    view.show();
    animation->start();

    return app.exec();
}

觉得有帮助的话,打赏一下呗。。

需要商务合作(定制程序)的欢迎私信!!

相关推荐
漫步企鹅1 天前
【VS Code - Qt】如何基于Docker Linux配置Windows10下的VS Code,开发调试ARM 版的Qt应用程序?
linux·qt·docker·arm·vs code·开发调试
pzzqq1 天前
buildroot编译qt 5.9.8 arm64版本踩坑
开发语言·qt
还债大湿兄1 天前
基于Qt Property Browser的通用属性系统:Any类与向量/颜色属性的完美结合
开发语言·qt
luciferau2 天前
Qt: WA_DontCreateNativeAncestors
qt
钱彬 (Qian Bin)2 天前
AI质检数据准备利器:基于Qt/QML 5.14的图像批量裁剪工具开发实战
qt·自定义·图像·qml·qt quick·裁剪工具
啊森要自信2 天前
【QT】常⽤控件详解(七)容器类控件 GroupBox && TabWidget && 布局管理器 && Spacer
linux·开发语言·c++·qt·adb
郝学胜-神的一滴3 天前
Horse3D引擎研发笔记(一):从使用Qt的OpenGL库绘制三角形开始
c++·qt·3d·unity·图形渲染·unreal engine
啊森要自信3 天前
【QT】常⽤控件详解(六)多元素控件 QListWidget && Table Widget && Tree Widget
c语言·开发语言·c++·qt
ZPC82104 天前
参数服务器 server and client
服务器·qt
牵牛老人4 天前
Qt中的QWebSocket 和 QWebSocketServer详解:从协议说明到实际应用解析
开发语言·qt·网络协议