Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect when mouse enter QGraphicsItem with button down

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?

like image 621
pafcu Avatar asked Oct 02 '10 12:10

pafcu


1 Answers

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)
like image 77
Rizo Avatar answered Nov 07 '22 14:11

Rizo