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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With