PyQt4: QGraphicsItem mousePressEvent() disables flag ItemIsMovable

試著忘記壹切 提交于 2020-02-05 13:38:52

问题


Here's a bug which I accidently solved it and have no idea why that works. I hope that someone could explain to me the logics behind it.

I've reimplmented QGraphicsItem and its mousePressEvent. By doing that the item was no longer movable. Even when trying to call QGraphicsItem.mousePressEvent(self, event) it didn't work. Only when I reimplmented mouseMoveEvent() and mouseReleaseEvent() it finally worked.

Code:

class LWResizeableItem(QtGui.QGraphicsItem):

    def __init__(self):

        super(LWResizeableItem, self).__init__()
        self.setFlag(QtGui.QGraphicsItem.ItemIsMovable)

    def mousePressEvent(self, event):
        QtGui.QGraphicsItem.mousePressEvent(self, event)
        < some code.... >

    def mouseMoveEvent(self, event):
        QtGui.QGraphicsItem.mouseMoveEvent(self, event)

    def mouseReleaseEvent(self, event):
        QtGui.QGraphicsItem.mouseReleaseEvent(self, event)

回答1:


A mouse event is propagated up the parent widget chain until a widget accepts it with accept(), or an event filter consumes it.

My guess, since you did not show relevant code, is that your mousePressEvent accepted the event. That prevented QtGui from handling it (Your code did all the handling).

You solved the "bug" by calling QtGui.QGraphicsItem.mousePressEvent which performs the default function (in addition to your own).

Adding the other two functions (mouseMoveEvent and mouseReleaseEvent) must have coincided with your adding the

QtGui.QGraphicsItem.mousePressEvent(self, event)

line to your mousePressEvent - that is why it seemed to have solved the problem.



来源:https://stackoverflow.com/questions/34884188/pyqt4-qgraphicsitem-mousepressevent-disables-flag-itemismovable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!