-1

I have a JSON file which contains coordinate list and some other information.

My JSON structure look like this;

            "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
                }
            ]

By taking the box values (box_top,box_left,box_height,box_width) I have drawn Rectangle using QGraphicsView and QGraphicsScene. The code is given below;

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()

Now What I want is when I click one box (say which has color red) from the GraphicsScene, I want to print its corresponding type and color. In a simple way, I want to print all data related to that box. A sample images also attached for reference. Note : Image is the output of this program.

Thank you.

Sravan JS
  • 41
  • 1
  • 7

1 Answers1

0

Use the code provided in the other answer I gave you (the second and last method are probably better) and get the data you need from the item. The "type" is the object class, the color is the pen() color.

musicamante
  • 41,230
  • 6
  • 33
  • 58
  • I have gone through that code. Can you just clarify the argument of ClickableGraphicsRectItem which you mentioned in your answer? That is "self.rect_item = ClickableGraphicsRectItem(...)". If I am calling only this with out "self.rect_item = self.scene.addRect(rect['box_top'],rect['box_left'],rect['box_width'],rect['box_length'],self.pen,self.brush)", I can not see any rectItem on Scene. Can you just help me with a sample code only for this part?I am new to PyQt5 so little confusion. – Sravan JS Dec 11 '19 at 03:27
  • I cannot test it right now, and frankly I don't exactly recall what I did (that was a month and a half ago), but I'm pretty sure that I tested it using your data, and it worked (to be honest, you also said that it was working "fine"). If you have issues with *that* answer, please comment there, not here. – musicamante Dec 11 '19 at 03:39