PySide(PyQt),QGraphicsItem的坐标映射转换函数
在PySide6中,QGraphicsItem
类提供了多个映射函数,用于在不同坐标系(例如:项目坐标系、父坐标系和场景坐标系)之间转换坐标和矩形。这些函数非常有用,尤其是在处理复杂的图形场景和需要精确位置信息的情况下。以下是每个函数的详细说明:
-
mapFromItem(self, item, point: QPoint | QPointF) -> QPointF
- 作用: 将
item
中的点转换到当前项目的坐标系中。 - 参数:
item
是源项目的QGraphicsItem
对象;point
是源项目的坐标系中的点(可以是QPoint
或QPointF
)。
- 作用: 将
-
mapFromParent(self, point: QPoint | QPointF) -> QPointF
- 作用: 将父项目中的点转换到当前项目的坐标系中。
- 参数:
point
是父项目的坐标系中的点(可以是QPoint
或QPointF
)。
-
mapFromScene(self, point: QPoint | QPointF) -> QPointF
- 作用: 将场景中的点转换到当前项目的坐标系中。
- 参数:
point
是场景的坐标系中的点(可以是QPoint
或QPointF
)。
-
mapRectFromItem(self, item, rect: QRect | QRectF) -> QRectF
- 作用: 将
item
中的矩形转换到当前项目的坐标系中。 - 参数:
item
是源项目的QGraphicsItem
对象;rect
是源项目的坐标系中的矩形(可以是QRect
或QRectF
)。
- 作用: 将
-
mapRectFromParent(self, rect: QRect | QRectF) -> QRectF
- 作用: 将父项目中的矩形转换到当前项目的坐标系中。
- 参数:
rect
是父项目的坐标系中的矩形(可以是QRect
或QRectF
)。
-
mapRectFromScene(self, rect: QRect | QRectF) -> QRectF
- 作用: 将场景中的矩形转换到当前项目的坐标系中。
- 参数:
rect
是场景的坐标系中的矩形(可以是QRect
或QRectF
)。
-
mapRectToItem(self, item, rect: QRect | QRectF) -> QRectF
- 作用: 将当前项目的矩形转换到
item
的坐标系中。 - 参数:
item
是目标项目的QGraphicsItem
对象;rect
是当前项目的坐标系中的矩形(可以是QRect
或QRectF
)。
- 作用: 将当前项目的矩形转换到
-
mapRectToParent(self, rect: QRect | QRectF) -> QRectF
- 作用: 将当前项目的矩形转换到父项目的坐标系中。
- 参数:
rect
是当前项目的坐标系中的矩形(可以是QRect
或QRectF
)。
-
mapRectToScene(self, rect: QRect | QRectF) -> QRectF
- 作用: 将当前项目的矩形转换到场景的坐标系中。
- 参数:
rect
是当前项目的坐标系中的矩形(可以是QRect
或QRectF
)。
-
mapToItem(self, item, point: QPoint | QPointF) -> QPointF
- 作用: 将当前项目的点转换到
item
的坐标系中。 - 参数:
item
是目标项目的QGraphicsItem
对象;point
是当前项目的坐标系中的点(可以是QPoint
或QPointF
)。
- 作用: 将当前项目的点转换到
-
mapToParent(self, point: QPoint | QPointF) -> QPointF
- 作用: 将当前项目的点转换到父项目的坐标系中。
- 参数:
point
是当前项目的坐标系中的点(可以是QPoint
或QPointF
)。
-
mapToScene(self, point: QPoint | QPointF) -> QPointF
- 作用: 将当前项目的点转换到场景的坐标系中。
- 参数:
point
是当前项目的坐标系中的点(可以是QPoint
或QPointF
)。
这些函数对于处理和转换不同图形元素之间的相对位置和尺寸非常有用。通过使用这些函数,你可以确保所有项目在同一个参考框架下正确渲染和计算,这对于构建复杂界面或进行精细化的图形操作非常重要。
from PySide6.QtWidgets import QApplication, QGraphicsScene, QGraphicsView, QGraphicsRectItem
from PySide6.QtCore import QPointF, QRectF
app = QApplication([])
scene = QGraphicsScene()
view = QGraphicsView(scene)
view.setSceneRect(0, 0, 640, 480)
rect_item = QGraphicsRectItem(0, 0, 50, 50) # 创建一个矩形
father_item = QGraphicsRectItem(0, 0, 100, 100) # 父项
grand_item = QGraphicsRectItem(0, 0, 200, 200) # 爷项
rect_item.setPos(50, 50) # 设置位置
father_item.setPos(100, 100) # 设置位置
rect_item.setParentItem(father_item) # 设置父项
father_item.setParentItem(grand_item) # 设置父项
pos = rect_item.mapToItem(grand_item, QPointF(25, 25))
print(pos) # 输出:QPointF(175, 175)
pos = rect_item.mapFromItem(grand_item, QPointF(150, 150))
print(pos) # 输出:QPointF(0, 0)
pos = rect_item.mapToScene(QPointF(25, 25))
print(pos) # 输出:QPointF(175, 175)
scene.addItem(grand_item)
rect = grand_item.mapRectFromItem(rect_item, QRectF(25, 25, 25, 25))
print(rect) # 输出:QRectF(175.000000, 175.000000, 25.000000, 25.000000)
rect = rect_item.mapRectFromParent(QRectF(25, 25, 25, 25))
print(rect) # QRectF(-25.000000, -25.000000, 25.000000, 25.000000)
view.show()
app.exec()