mousePressEvent not working as expected in QGraphicsScene

103 views Asked by At

I've been working on reimplementing a QGraphicsItem, but when the item is clicked, further functions I try to run with that class only affect the instance that was first clicked. My question is, how do I get around this, and only affect the instance that is explicitly clicked?

My class is as follows:

class PrefabPoly(QGraphicsPolygonItem):

    def __init__(self, points, pen, brush, graphicsItem, parent=None):
        super(PrefabPoly, self).__init__(QPolygon(points), graphicsItem, parent.scene)
        self.setPen(pen)
        self.setBrush(brush)
        self.setCursor(Qt.PointingHandCursor)
        self.parent = parent

    def mousePressEvent(self, e):
        print("press")
        if isinstance(self.parent, CreatePrefabGridWidget):
            self.setBrush(self.parent.cur_color) 
        #The brush is only changed for the item that was clicked first!?!?
1

There are 1 answers

0
Jonathan Liu On BEST ANSWER

This was fixed by calling the original class function instead of totally overriding it.

In the mousePressEvent:

def mousePressEvent(self, e):
    print("press")
    if isinstance(self.parent, CreatePrefabGridWidget):
        self.setBrush(self.parent.cur_color)

    QGraphicsPolygonItem.mousePressEvent(self, e)