单击可选择的GraphicsItem时如何打印数据?

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

我有一个包含坐标列表和一些其他信息的JSON文件。

我的JSON结构看起来像这样;

            "annotations": [
                {
                    "type": "Box",
                    "color": "red",
                    "box_top": 406.0,
                    "box_left": 656.0,
                    "box_height": 73.0,
                    "box_width": 40.0
                }
            ],

            "annotations": [
                {
                    "type": "Box",
                    "color": "green",
                    "box_top": 450.0,
                    "box_left": 700.0,
                    "box_height": 95.0,
                    "box_width": 47.0
                }
            ]

通过获取框值(box_top,box_left,box_height,box_width),我使用QGraphicsView和QGraphicsScene绘制了Rectangle。代码如下:

def load_image(self,image_item):
    self.scene = QGraphicsScene(self.centralWidget) # Created a QGraphicsScene
    self.pic = QPixmap(str(image_item.text())) # Loaded Image

    self.brush = QBrush()
    self.pen = QPen(Qt.red)
    self.pen.setWidth(2)
    self.pixItem = QGraphicsPixmapItem(self.pic)
    self.load_view = self.scene.addItem(self.pixItem) # Image Added to Scene

    # Opening JSON and fetching data
    # …
    for rect in json_file['annotations']:

        # Taken type and color and stored in variable

        self.box_type = rect['type']
        self.box_color = rect['color']

        # Taken box_top,box_left,box_height,box_width values

        self.rect_item = self.scene.addRect(rect['box_top'],rect['box_left'],rect['box_width'],rect['box_length'],self.pen,self.brush) # x,y,w,h
        self.rect_item.setFlag(QGraphicsItem.ItemIsSelectable) #Item is Selectable
        #self.rect_item.setFlag(QGraphicsItem.ItemIsMovable) # Item is Movable

    self.fit_view = self.gView.setScene(self.scene)
    self.gView.fitInView(self.pixItem,Qt.KeepAspectRatio)
    self.gView.setRenderHint(QPainter.Antialiasing)
    self.gView.show()

现在我想要的是,当我单击GraphicsScene中的一个框(例如,具有红色的框)时,我想打印其相应的类型和颜色。我想以一种简单的方式来打印与该框有关的所有数据。还附有样本图像供参考。注意:图像是该程序的输出。

“”

谢谢。

python python-3.x pyqt5 qgraphicsview qgraphicsscene
1个回答
0
投票

使用其他answer I gave you中提供的代码(第二和最后一种方法可能更好),并从item中获取所需的数据。 [类型]是对象类,颜色是pen()颜色。

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