PyQT5 setRect 在 QGraphicsScene 中移动原点
PyQT5 setRect moves origin point in a QGraphicsScene
每当我尝试在场景中移动矩形时,矩形的原点似乎会更改为矩形在更新位置之前所在的位置。
因此,如果我在 (0,0) 处创建矩形并使用 rect.setRect(x, y) 移动它,那么 returning 位置将产生 (0,0) 而不是 ( x, y).
如果您使用鼠标在 QGraphicsScene 中移动它,它将 return 正确的 (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)
为了移动这个矩形,我有以下嵌入函数和一个到 return 位置的函数:
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)
看来我想通了
我将移动函数更改为以下内容:
def move(self, x, y):
"""Moves the object in the editor view to coordinatex x,y"""
self.rect.setPos(x, y)
那个returns场景内的正确位置适合我!
无论如何谢谢:)
您忘记了图形项目的一个重要方面:它们的 [场景] 位置并不总是实际显示给用户的对象的左上角。
这在使用 scene.add*()
添加项目时很明显(这已在 上进行了解释)。
作为 documentation explains:
Note that the item's geometry is provided in item coordinates, and its position is initialized to (0, 0). For example, if a QRect(50, 50, 100, 100) is added, its top-left corner will be at (50, 50) relative to the origin in the item's coordinate system.
item 位置不是 rectangle 位置,所以当你使用 setRect
时你并没有移动项目,但在指定位置设置一个新矩形,同时将项目留在其系统的 (0, 0) 坐标处;请注意,这也意味着如果该项目没有父级,scenePos()
将与 pos()
相同,否则它是相对于父级的。
如果您想知道矩形左上角的实际位置,可以使用以下方法之一:
item.sceneBoundingRect().topLeft()
item.scenePos() + item.rect().topLeft()
如果你总是在 (0, 0) 添加矩形,那么你可以只使用 setPos()
,但是如果你需要根据当前的实际矩形位置计算位置,你必须使用上述功能之一。
注意矩形也可以有负尺寸,所以如果你需要可见矩形的左上角,你需要normalize它:
item.scenePos() + item.rect().normalized().topLeft()
每当我尝试在场景中移动矩形时,矩形的原点似乎会更改为矩形在更新位置之前所在的位置。
因此,如果我在 (0,0) 处创建矩形并使用 rect.setRect(x, y) 移动它,那么 returning 位置将产生 (0,0) 而不是 ( x, y).
如果您使用鼠标在 QGraphicsScene 中移动它,它将 return 正确的 (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)
为了移动这个矩形,我有以下嵌入函数和一个到 return 位置的函数:
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)
看来我想通了
我将移动函数更改为以下内容:
def move(self, x, y):
"""Moves the object in the editor view to coordinatex x,y"""
self.rect.setPos(x, y)
那个returns场景内的正确位置适合我! 无论如何谢谢:)
您忘记了图形项目的一个重要方面:它们的 [场景] 位置并不总是实际显示给用户的对象的左上角。
这在使用 scene.add*()
添加项目时很明显(这已在
作为 documentation explains:
Note that the item's geometry is provided in item coordinates, and its position is initialized to (0, 0). For example, if a QRect(50, 50, 100, 100) is added, its top-left corner will be at (50, 50) relative to the origin in the item's coordinate system.
item 位置不是 rectangle 位置,所以当你使用 setRect
时你并没有移动项目,但在指定位置设置一个新矩形,同时将项目留在其系统的 (0, 0) 坐标处;请注意,这也意味着如果该项目没有父级,scenePos()
将与 pos()
相同,否则它是相对于父级的。
如果您想知道矩形左上角的实际位置,可以使用以下方法之一:
item.sceneBoundingRect().topLeft()
item.scenePos() + item.rect().topLeft()
如果你总是在 (0, 0) 添加矩形,那么你可以只使用 setPos()
,但是如果你需要根据当前的实际矩形位置计算位置,你必须使用上述功能之一。
注意矩形也可以有负尺寸,所以如果你需要可见矩形的左上角,你需要normalize它:
item.scenePos() + item.rect().normalized().topLeft()