Pyqt5 QGraphicsPixmapItem未正确旋转
我需要将一个Pyqt5 QGraphicsPixmapItem未正确旋转,pyqt5,image-rotation,qgraphicsitem,qgraphicspixmapitem,Pyqt5,Image Rotation,Qgraphicsitem,Qgraphicspixmapitem,我需要将一个QGraphicsPixmapItem旋转一圈。也就是说,圆始终需要位于图像的左上角,当我拖动圆时,图像必须旋转。我使用setRotation设置旋转角度,并使用setTransformOriginPoint设置旋转点。但是,旋转工作不正常。有人能给我指一下正确的方向吗 导入系统 从PyQt5.QtWidgets导入QMainWindow、QApplication、QGraphicsView 从PyQt5导入QtGui、QtWidgets、QtCore 输入数学 类主窗口(QMain
QGraphicsPixmapItem
旋转一圈。也就是说,圆始终需要位于图像的左上角,当我拖动圆时,图像必须旋转。我使用setRotation
设置旋转角度,并使用setTransformOriginPoint
设置旋转点。但是,旋转工作不正常。有人能给我指一下正确的方向吗
导入系统
从PyQt5.QtWidgets导入QMainWindow、QApplication、QGraphicsView
从PyQt5导入QtGui、QtWidgets、QtCore
输入数学
类主窗口(QMainWindow):
def uuu init uuu(self,parent=None):
超级(主窗口,自我)。\uuuuu初始化\uuuuuuu(父级)
self.scene=scene()
self.view=QGraphicsView(self)
自身设置几何体(10、30、850、600)
self.view.setGeometry(20、22、800、550)
自设置固定大小(850600)
self.view.setScene(self.scene)
类场景(qtwidts.qgraphicscene):
def uuu init uuu(self,parent=None):
超级(场景,自我)。\uuuuu初始\uuuuuuu(父级)
#这里还有其他东西
self.set_image()
def set_图像(自身):
image=image()
self.addItem(图像)
image.set_pixmap()
类映像(qtwidts.QGraphicsPixmapItem):
def uuu init uuu(self,parent=None):
超级(映像,自我)。\uuuuu初始化\uuuuuuuu(父级)
self.setFlag(qtwidts.QGraphicsItem.ItemIsMovable,True)
self.setTransformOriginPoint(self.boundingRect().center())
def set_像素映射(自):
pixmap=QtGui.QPixmap(“image.jpg”)
self.setPixmap(pixmap)
self.pixmap_controller=PixmapController(self)
self.pixmap\u controller.set\u pixmap\u controller()
类PixmapController(QtWidgets.qGraphicsSellipseitem):
定义初始化(self,pixmap):
超级(PixmapController,self)。\uuuu init\uuuuu(父级=pixmap)
self.pixmap=pixmap
color=QtGui.QColor(0,0,0)
brush=QtGui.QBrush(颜色)
自我挫折(刷子)
def set_pixmap_控制器(自身):
self.setRect(-5,-5,10,10)
def鼠标压力事件(自身、事件):
如果event.button()==QtCore.Qt.LeftButton:
self.startPos=event.pos()
def mouseMoveEvent(自身、事件):
如果event.buttons()==QtCore.Qt.LeftButton:
self.finalPos=event.pos()
delta=self.finalPos-self.startPos
item_position=self.pixmap.transformOriginPoint()
angle=math.atan2(item_position.y()-self.finalPos.y(),item_position.x()-self.finalPos.x())/math.pi*180-45
self.parentItem().setRotation(角度)
self.parentItem().setPos(self.parentItem().pos()+delta)
如果名称=“\uuuuu main\uuuuuuuu”:
app=QApplication(sys.argv)
窗口=主窗口()
window.show()
sys.exit(app.exec_())
与前一个问题一样,你必须考虑项目坐标系是基于它的父(或场景,如果没有)。 您的代码还有两个问题:
QPointF()
,如项目左上角所示),因为此时边界矩形为null;这将使转换不一致,因为原点始终位于pixmap项的左上角,而不是其中心李>
set\u pixmap\u controller
这样的专用函数及其相关调用没有任何用处,只需在\u init\u
C中使用setRect()