2016-12-18 1 views
0

QGraphicsItem을 다시 구현하고 있지만 항목을 클릭하면 해당 클래스로 실행하려고하는 추가 기능은 처음 클릭 한 인스턴스에만 영향을줍니다. 내 질문은이 문제를 어떻게 해결할 수 있으며 명시 적으로 클릭 한 인스턴스에만 영향을 미칩니 까?QGraphicsScene에서 mousePressEvent가 예상대로 작동하지 않습니다

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

이 원래 클래스의 함수를 호출하는 대신 완전히를 재정 의하여 고정 다음과 같이

내 클래스입니다. mousePressEvent에서

:

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

    QGraphicsPolygonItem.mousePressEvent(self, e)