子项未使用父坐标

问题描述 投票:0回答:1

我的 PySide 坐标系有问题。我创建一个继承自 QGraphicsRectItem 的自定义项 (NodeItem),并将其放置在场景中。该自定义项有一个继承自 QGraphicsTextItem 的子项 (LabelItem)。当我将父项 (NodeItem) 添加到场景时,其位于场景坐标中的 150,150 中,但子 LabelItem 位于场景坐标中,而不是位于其父坐标 (NodeItem) 中。

如图所示,“R1”LabelItem 位于场景的原点,而不是 NodeItem(矩形)的原点。

from PySide6.QtWidgets import QGraphicsRectItem, QGraphicsTextItem, QGraphicsScene, QGraphicsView, QApplication
from PySide6.QtGui import QColor, QPen, QBrush
from PySide6.QtCore import QRectF
from PySide6 import QtWidgets

class LabelItem(QtWidgets.QGraphicsTextItem):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setFlags(QtWidgets.QGraphicsItem.ItemIsMovable | QtWidgets.QGraphicsItem.ItemIsSelectable)
        self.setZValue(2)

class NodeItem(QGraphicsRectItem):
    def __init__(self, x, y, width, height):
        super().__init__(x, y, width, height)

     
        pen = QPen(QColor("#000000"))  
        brush = QBrush(QColor("#FFD700"))  
        self.setPen(pen)
        self.setBrush(brush)
        self.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable)
        self.setFlag(QtWidgets.QGraphicsItem.ItemIsSelectable)
        self.setFlag(QtWidgets.QGraphicsItem.ItemIsFocusable)
        self.setFlag(QtWidgets.QGraphicsItem.ItemSendsGeometryChanges)

        # LabelItem created as a child of this item
        self.label = LabelItem(self) #Child item
        self.label.setPlainText("R1")
        self.update_label_position()
        print(self.label.parentItem())
    def update_label_position(self):
        
        self.label.setPos(0, 0)  #Child item positioned at 0,0
    
class MyGraphicsView(QGraphicsView):
    def __init__(self):
        super().__init__()
        self.setScene(QGraphicsScene(self))
        self.setMouseTracking(True)

        # Node item creation and positioned at 150,150 of the scene
        node_item = NodeItem(150, 150, 100, 50)
        self.scene().addItem(node_item)

if __name__ == "__main__":
    app = QApplication([])

    view = MyGraphicsView()
    view.setSceneRect(0, 0, 400, 300)
    view.show()

    app.exec()

enter image description here

python pyside6
1个回答
0
投票

我将类从 QGraphicsRectItem 更改为 QGraphicsPixmapItem 并且它有效。似乎存在冲突,至少在这种情况下,当使用父坐标定位 QGraphicsRectItem 中的子项时,文档对此没有任何说明。

from PySide6.QtWidgets import  QGraphicsTextItem, QGraphicsScene, QGraphicsView, QApplication

from PySide6.QtCore import QRectF
from PySide6 import QtWidgets, QtSvg

class LabelItem(QtWidgets.QGraphicsTextItem):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setFlags(QtWidgets.QGraphicsItem.ItemIsMovable | QtWidgets.QGraphicsItem.ItemIsSelectable)
        self.setZValue(2)


class NodeItem(QtWidgets.QGraphicsPixmapItem):
    def __init__(self, svg_file, x, y):
        super().__init__(svg_file)
        self.setPos(x, y)  # Position the item in the scene

        self.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable)
        self.setFlag(QtWidgets.QGraphicsItem.ItemIsSelectable)
        self.setFlag(QtWidgets.QGraphicsItem.ItemIsFocusable)
        self.setFlag(QtWidgets.QGraphicsItem.ItemSendsGeometryChanges)

        # LabelItem created as a child of this item
        self.label = LabelItem(self)  # Child item
        self.label.setPlainText("R1")
        self.update_label_position()

    def update_label_position(self):
        self.label.setPos(0,0)  # Center the label in the rect


class MyGraphicsView(QGraphicsView):
    def __init__(self):
        super().__init__()
        self.setScene(QGraphicsScene(self))
        self.setMouseTracking(True)
        from PySide6.QtGui import QPixmap

        # Node item creation and positioned at 150, 150 of the scene
        node_item = NodeItem( QPixmap("add-note.svg"), 150, 150)
        self.scene().addItem(node_item)


if __name__ == "__main__":
    app = QApplication([])

    view = MyGraphicsView()
    view.setSceneRect(0, 0, 400, 300)
    view.show()

    app.exec()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.