Search code examples
pythonmouseeventpyqt4polygonqgraphicsview

MousePressEvent catches signal unnecessary- PyQt4


So I have a QraphicsScene with a QGraphicsPolygonItem , which I flag as movable. And I also override the MousePressEvent. My code snippet tll now.

def mousePressEvent(self , e):
    self.endx = e.x()
    self.endy = e.y()
    if self.sender == 1:
        self.LineChange(self.endx , self.endy)

#...

def CreateFun(self):
    poly = QtGui.QPolygonF([QtCore.QPointF(100 , 100) , QtCore.QPointF(100 , 200) , QtCore.QPointF(200 , 200)])
    self.polygon = QtGui.QGraphicsPolygonItem(poly)
    self.scene.addItem(self.polygon)
    self.polygon.setFlag(QtGui.QGraphicsItem.ItemIsMovable)

However the polygon isn't moving . And when I comment out the MousePressEvent , it moves fine . My guess is that the MousePressEvent , catches it before the PolygonItem does.

And the above functions are from a class inherited from QtGui.QGraphicsView. Any suggestions?


Solution

  • You should call the base implementation for the mousePressEvent. QGraphicsView normally passes these clicks to other items that might use them. If you don't call the base implementation you're basically 'trapping' the click.

    Modify your mousePressEvent as following:

    def mousePressEvent(self , e):
        self.endx = e.x()
        self.endy = e.y()
        if self.sender == 1:
            self.LineChange(self.endx , self.endy)
        # let the base implementation do its thing
        super(Palette, self).mousePressEvent(e)