PyQt5:GraphicsScene 的所有项目都有坐标 0.0
PyQt5: All Items of GraphicsScene have coordinates 0.0
我使用了下面的 并稍微修改了一下,得到了下面的小例子:
import sys
from PyQt5 import QtCore, QtWidgets
class GraphicsScene(QtWidgets.QGraphicsScene):
def __init__(self):
super(GraphicsScene, self).__init__()
self.setSceneRect(0, 0, 600, 400)
def mousePressEvent(self, event):
if event.buttons() == QtCore.Qt.LeftButton:
x = event.scenePos().x()
y = event.scenePos().y()
self.addRect(x, y, 100, 100)
elif event.buttons() == QtCore.Qt.RightButton:
for elem in self.items():
print(elem.x())
super(GraphicsScene, self).mousePressEvent(event)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
scene = GraphicsScene()
w = QtWidgets.QGraphicsView(scene)
w.resize(610, 410)
w.show()
sys.exit(app.exec_())
想法是,通过单击鼠标左键创建新的矩形(这已经有效)并通过单击鼠标右键删除最近的矩形。我知道如何找到最近的矩形,但为此我需要现有矩形的坐标。如果我们向场景中添加一个新的矩形,我们将执行以下操作:
self.addRect(x, y, 100, 100)
但是如果我遍历场景中的所有元素,并尝试使用以下方法获取元素的 x 坐标:
for elem in self.items():
print(elem.x())
print(elem.pos().x())
print(elem.scenePos().x())
那么所有的打印输出都是零。我已经看过 docu,但据我了解,我正在按照文档的建议进行操作。你知道我做错了什么吗?
编辑:
当然,我可以将所有坐标保存在一个附加列表中,使用该列表中的值计算最近的矩形,使用以下方法删除每个矩形:
for elem in self.items():
self.removeItem(elem)
并绘制剩余的矩形。但是,我希望有一个更干净的版本。 :)
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.
所以有两个选择:
- 在位置 (0, 0) 添加指定大小的矩形,然后将其移动到所需位置:
rectItem = self.addRect(0, 0, 100, 100)
rectItem.setPos(x, y)
- 使用addRect中的坐标,根据矩形的左上角得到实际位置:
for elem in self.items():
pos = elem.pos()
if isinstance(elem, QtWidgets.QGraphicsRectItem):
pos += elem.rect().topLeft()
我使用了下面的
import sys
from PyQt5 import QtCore, QtWidgets
class GraphicsScene(QtWidgets.QGraphicsScene):
def __init__(self):
super(GraphicsScene, self).__init__()
self.setSceneRect(0, 0, 600, 400)
def mousePressEvent(self, event):
if event.buttons() == QtCore.Qt.LeftButton:
x = event.scenePos().x()
y = event.scenePos().y()
self.addRect(x, y, 100, 100)
elif event.buttons() == QtCore.Qt.RightButton:
for elem in self.items():
print(elem.x())
super(GraphicsScene, self).mousePressEvent(event)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
scene = GraphicsScene()
w = QtWidgets.QGraphicsView(scene)
w.resize(610, 410)
w.show()
sys.exit(app.exec_())
想法是,通过单击鼠标左键创建新的矩形(这已经有效)并通过单击鼠标右键删除最近的矩形。我知道如何找到最近的矩形,但为此我需要现有矩形的坐标。如果我们向场景中添加一个新的矩形,我们将执行以下操作:
self.addRect(x, y, 100, 100)
但是如果我遍历场景中的所有元素,并尝试使用以下方法获取元素的 x 坐标:
for elem in self.items():
print(elem.x())
print(elem.pos().x())
print(elem.scenePos().x())
那么所有的打印输出都是零。我已经看过 docu,但据我了解,我正在按照文档的建议进行操作。你知道我做错了什么吗?
编辑: 当然,我可以将所有坐标保存在一个附加列表中,使用该列表中的值计算最近的矩形,使用以下方法删除每个矩形:
for elem in self.items():
self.removeItem(elem)
并绘制剩余的矩形。但是,我希望有一个更干净的版本。 :)
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.
所以有两个选择:
- 在位置 (0, 0) 添加指定大小的矩形,然后将其移动到所需位置:
rectItem = self.addRect(0, 0, 100, 100)
rectItem.setPos(x, y)
- 使用addRect中的坐标,根据矩形的左上角得到实际位置:
for elem in self.items():
pos = elem.pos()
if isinstance(elem, QtWidgets.QGraphicsRectItem):
pos += elem.rect().topLeft()