问题
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