Detect when mouse enters QGraphicsItem with button down

I want to determine when the mouse cursor moves during QGraphicsItem while the mouse button is pressed, that is, the button is pressed before the mouse enters the element. My first idea was to use hoverEnterEvent , but when I left-click it fails. My other idea was to use dragEnterEvent , but it doesn't seem to work at all (although I used setAcceptDrops(True) .

What is the best way to detect when the cursor moves over an item and the mouse button is pressed?

+4
source share
2 answers

I just found this question, I know that it is old, but I hope that my answer will be useful for someone with this problem.

In the QGraphicsView or QGraphicsScene derived class, override the mouseMoveEvent method and check the buttons event property to see which buttons are currently pressed. Here is a sample 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) 
+3
source

Try mouseMoveEvent() and mousePressEvent() . If they do not help you, you need to override the virtual method

 bool QGraphicsItem::sceneEvent ( QEvent * event ) 

Check the state of the mouse button and call the appropriate event handler.

0
source

All Articles