当前位置: 首页 > article >正文

PySide(PyQt),QGraphicsItem的坐标映射转换函数

        在PySide6中,QGraphicsItem 类提供了多个映射函数,用于在不同坐标系(例如:项目坐标系、父坐标系和场景坐标系)之间转换坐标和矩形。这些函数非常有用,尤其是在处理复杂的图形场景和需要精确位置信息的情况下。以下是每个函数的详细说明:

  1. mapFromItem(self, item, point: QPoint | QPointF) -> QPointF

    • 作用: 将 item 中的点转换到当前项目的坐标系中。
    • 参数item 是源项目的 QGraphicsItem 对象;point 是源项目的坐标系中的点(可以是 QPoint 或 QPointF)。
  2. mapFromParent(self, point: QPoint | QPointF) -> QPointF

    • 作用: 将父项目中的点转换到当前项目的坐标系中。
    • 参数point 是父项目的坐标系中的点(可以是 QPoint 或 QPointF)。
  3. mapFromScene(self, point: QPoint | QPointF) -> QPointF

    • 作用: 将场景中的点转换到当前项目的坐标系中。
    • 参数point 是场景的坐标系中的点(可以是 QPoint 或 QPointF)。
  4. mapRectFromItem(self, item, rect: QRect | QRectF) -> QRectF

    • 作用: 将 item 中的矩形转换到当前项目的坐标系中。
    • 参数item 是源项目的 QGraphicsItem 对象;rect 是源项目的坐标系中的矩形(可以是 QRect 或 QRectF)。
  5. mapRectFromParent(self, rect: QRect | QRectF) -> QRectF

    • 作用: 将父项目中的矩形转换到当前项目的坐标系中。
    • 参数rect 是父项目的坐标系中的矩形(可以是 QRect 或 QRectF)。
  6. mapRectFromScene(self, rect: QRect | QRectF) -> QRectF

    • 作用: 将场景中的矩形转换到当前项目的坐标系中。
    • 参数rect 是场景的坐标系中的矩形(可以是 QRect 或 QRectF)。
  7. mapRectToItem(self, item, rect: QRect | QRectF) -> QRectF

    • 作用: 将当前项目的矩形转换到 item 的坐标系中。
    • 参数item 是目标项目的 QGraphicsItem 对象;rect 是当前项目的坐标系中的矩形(可以是 QRect 或 QRectF)。
  8. mapRectToParent(self, rect: QRect | QRectF) -> QRectF

    • 作用: 将当前项目的矩形转换到父项目的坐标系中。
    • 参数rect 是当前项目的坐标系中的矩形(可以是 QRect 或 QRectF)。
  9. mapRectToScene(self, rect: QRect | QRectF) -> QRectF

    • 作用: 将当前项目的矩形转换到场景的坐标系中。
    • 参数rect 是当前项目的坐标系中的矩形(可以是 QRect 或 QRectF)。
  10. mapToItem(self, item, point: QPoint | QPointF) -> QPointF

    • 作用: 将当前项目的点转换到 item 的坐标系中。
    • 参数item 是目标项目的 QGraphicsItem 对象;point 是当前项目的坐标系中的点(可以是 QPoint 或 QPointF)。
  11. mapToParent(self, point: QPoint | QPointF) -> QPointF

    • 作用: 将当前项目的点转换到父项目的坐标系中。
    • 参数point 是当前项目的坐标系中的点(可以是 QPoint 或 QPointF)。
  12. 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()


http://www.kler.cn/a/590511.html

相关文章:

  • 【Unity3D】Addressables使用流程
  • 使用Electron Forge打包serialport模块的详细指南
  • Go语言的智能合约
  • PlainUSR|LIA: 追求更快的卷积网络实现高效的超分辨率重建
  • 在用Docker配置Redis哨兵节点的时候出现的错误及其解决办法
  • 【Python 算法零基础 1.线性枚举】
  • DeepSeek私有化部署与安装浏览器插件内网穿透远程访问实战
  • ISP--Gamma Correction
  • 搞定python之八----操作mysql
  • 【SpringMVC】常用注解:@SessionAttributes
  • Qt QML解决SVG图片显示模糊的问题
  • @RequestBody注解解释
  • 服务创造未来 东隆科技携多款产品亮相慕尼黑
  • 【网络协议】基于UDP的可靠协议:KCP
  • Git 使用指南
  • 【多线程】单例模式
  • Unity学习之Shader总结(一)
  • Docker入门篇2:查看容器、运行容器、启动和停止容器、删除容器
  • Android PC 要来了?Android 16 Beta3 出现 Enable desktop experience features 选项
  • 【STM32】NVIC(嵌套向量中断控制器)