【发布时间】:2020-09-29 13:49:36
【问题描述】:
每当我尝试在场景中移动矩形时,矩形的原点似乎会更改为矩形在更新位置之前所在的位置。
因此,如果我在 (0,0) 处创建矩形并使用 rect.setRect(x, y) 移动它,则返回该位置将产生 (0,0) 而不是 (x, y)。
如果您使用鼠标在 QGraphicsScene 中移动它,它将返回正确的 (x, y)。
我用来创建矩形的代码如下:
class placeableObject:
def __init__(self, index, scene, QPen, QBrush, width=100, height=100):
"""Parent class for placeable objects"""
self.width = float(width)
self.height = float(height)
self.index = index
self.rect = scene.addRect(0, 0, int(width), int(height), QPen, QBrush)
self.rect.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable)
要移动这个矩形,我有以下嵌入函数和返回位置的函数:
def getPos(self):
"""Returns a list with the x,y position of the object in the scene"""
return [self.rect.scenePos().x(), self.rect.scenePos().y()]
def move(self, x, y):
"""Moves the object in the editor view to coordinatex x,y"""
self.rect.setRect(x, y, self.width, self.height)
【问题讨论】:
标签: python pyqt5 qgraphicsscene