我一直在努力重新实现QGraphicsItem
,但是当单击该项时,我尝试使用该类运行的其他函数仅影响首次单击的实例。我的问题是,如何解决这个问题,并且只影响明确点击的实例?
我的课程如下:
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!?!?
答案 0 :(得分:0)
通过调用原始类函数而不是完全覆盖它来解决这个问题。
在mousePressEvent中:
def mousePressEvent(self, e):
print("press")
if isinstance(self.parent, CreatePrefabGridWidget):
self.setBrush(self.parent.cur_color)
QGraphicsPolygonItem.mousePressEvent(self, e)