QWidget旋转方法

参考链接:https://stackoverflow.com/questions/43388464/rotate-whole-qwidget-by-angle
说明:
本文实现方法本质是使用QGraphics三件套,即View、Scene、Item,将QWidget控件作为Item显示,从而实现角度控制的目的,基于QGraphics框架,除了角度控制外还可以对Item进行移动、缩放等等操作。

个人觉得QGraphics框架使用还是比较占资源和徒增代码量的,如果你的项目中只是一个非常小的控件需要旋转显示,个人还是建议继承QWidget然后实现自绘虚函数,实现旋转的目的。

1、显示效果
在这里插入图片描述
2、最简源代码
首先,ui设计界面中拖一个 Graphics View到你的界面中,然后mainwindows构造函数中输入如下代码:

#include <QGraphicsScene>
#include <QGraphicsProxyWidget>
#include <QPushButton>

QGraphicsScene *scene = new QGraphicsScene(this);
QPushButton *button = new QPushButton();
button->setText("45度的酒");

QGraphicsProxyWidget *w = scene->addWidget(button);
w->setPos(50, 50);
w->setRotation(45);
ui->graphicsView->setScene(scene);

3、相关类的了解
QGraphicsScene 称为图形场景,QGraphicsView 称为图形窗口。
QGraphicsView为图形窗口,就是可视化的界面,就如同是一台电视机的外壳;
QGraphicsScene图形场景,为电视机的屏幕,而要播放的内容是要在屏幕里显示出来的。
注:QGraphicsScene图形窗口 都有自己的无视觉外观,它只管理项目。你需要创建一个QGraphicsView图形视图 加载 部件可视化的场景。
QGraphicsScene学习链接: https://blog.csdn.net/wishfly/article/details/79356053
QGraphicsView学习链接:https://blog.csdn.net/qq_34139994/article/details/105301214

其他推荐内容:

  • 5
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要给QWidget添加顺时针旋转动画,可以使用QPropertyAnimation类和QTransform类。 首先,使用QTransform类创建一个旋转矩阵,然后将其应用到QWidget上。这可以通过QWidget的setTransform()方法实现,如下所示: ```python from PyQt5.QtCore import QPropertyAnimation, QPointF, QParallelAnimationGroup from PyQt5.QtGui import QTransform from PyQt5.QtWidgets import QWidget, QApplication class MyWidget(QWidget): def __init__(self): super().__init__() self.setGeometry(100, 100, 200, 200) self.show() def animate(self): # 创建旋转矩阵 transform = QTransform() transform.rotate(90) # 创建属性动画 animation = QPropertyAnimation(self, b"transform") animation.setDuration(1000) animation.setStartValue(self.transform()) animation.setEndValue(transform) # 启动动画 animation.start() if __name__ == '__main__': app = QApplication([]) widget = MyWidget() widget.animate() app.exec_() ``` 在上面的代码中,我们创建了一个MyWidget类作为QWidget的子类,并重写了其animate()方法,用于启动旋转动画。 在animate()方法中,我们首先使用QTransform类创建了一个旋转矩阵,将其应用到QWidget上。然后,我们使用QPropertyAnimation类创建了一个属性动画,将其绑定到QWidget上的transform属性上。最后,我们启动动画并等待其完成。 需要注意的是,由于QWidget的transform属性是只读的,因此我们需要使用Q_PROPERTY宏将其声明为可写属性,如下所示: ```python class MyWidget(QWidget): Q_PROPERTY(QTransform transform READ transform WRITE setTransform) def __init__(self): super().__init__() self.setGeometry(100, 100, 200, 200) self.show() def animate(self): # 创建旋转矩阵 transform = QTransform() transform.rotate(90) # 创建属性动画 animation = QPropertyAnimation(self, b"transform") animation.setDuration(1000) animation.setStartValue(self.transform()) animation.setEndValue(transform) # 启动动画 animation.start() def setTransform(self, transform): self._transform = transform self.update() def transform(self): return self._transform ``` 在上面的代码中,我们在MyWidget类中添加了setTransform()和transform()方法,并使用Q_PROPERTY宏将其声明为可写属性。setTransform()方法用于将传入的旋转矩阵赋值给_transform属性,并调用update()方法刷新QWidget。transform()方法用于返回_transform属性的值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值