I want to detect when the mouse cursor moves in over a QGraphicsItem
while a mouse button is pressed, i.e. the button is pressed before the mouse enters the item. My first idea was to use hoverEnterEvent
, but it doesn't seem to trigger when the left mouse button is pressed. My other idea was to use dragEnterEvent
, but it doesn't seem to trigger at all (even though I have used setAcceptDrops(True)
.
What is the best way to detect when the cursor moves on top of an item and the mouse button is pressed?
I just found this question, I know it's old, but I hope my answer will be useful for someone with this problem.
In the QGraphicsView
or QGraphicsScene
derived class override the mouseMoveEvent
method and check the event's buttons
property to know what buttons are currently pressed. Here's an example code in PyQt4 from a small project I'm working on:
def mouseMoveEvent(self, event):
buttons = event.buttons()
pos = self.mapToScene(event.pos())
object = self.scene().itemAt(pos)
type = EventTypes.MouseLeftMove if (buttons & Qt.LeftButton) else\
EventTypes.MouseRightMove if (buttons & Qt.RightButton) else\
EventTypes.MouseMidMove if (buttons & Qt.MidButton) else\
EventTypes.MouseMove
handled = self.activeTool().handleEvent(type, object, pos)
if (not handled):
QGraphicsView.mouseMoveEvent(self, event)
Try the mouseMoveEvent()
and mousePressEvent()
. If they do not help you then you'll need to reimplement virtual method
bool QGraphicsItem::sceneEvent ( QEvent * event )
Check the mouse button state inside and call the appropriate event handler.