if event.button() == Qt.RightButton:
itemFromCoor = self.itemAt(event.pos())
if isinstance(itemFromCoor, ButtonCancel):
father = itemFromCoor.parentItem()
self.scene.removeItem(father)
In my code, this is within the mousePressEvent event in the QGraphicsView, and what it does is when the right mouse button has been pressed, it uses itemAt with the mouse position to capture the item from the view, the items in the scene. The parent item is the rectangle, and the child item is the "X" rectangle; both items are custom classes derived from QGraphicsRectItem. If the child item ("X") is clicked, both the parent and the child itself are supposed to be deleted. My question is: when I want to delete a rectangle as shown in the image, it doesn't get deleted by the itemAt function.

When I right-click on these three "X" rectangles, they do not get deleted because what the itemAt() function returns is the rectangle that is marked with a yellow arrow:

How can I solve this problem?