mousePressEvent 在 QGraphicsScene 中没有按预期工作

mousePressEvent not working as expected in QGraphicsScene

我一直在努力重新实现 QGraphicsItem,但是当单击该项目时,我尝试 运行 的更多功能 class 只会影响第一个实例点击。我的问题是,如何解决这个问题,并且只影响明确点击的实例?

我的class如下:

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!?!?

这已通过调用原始 class 函数而不是完全覆盖它来解决。

在mousePressEvent中:

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

    QGraphicsPolygonItem.mousePressEvent(self, e)