Detect when mouse enter QGraphicsItem with button down
Asked Answered
S

2

6

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?

Scully answered 2/10, 2010 at 12:12 Comment(2)
You may have to filter the mouse events at the graphics scene level, and add some notification to each widget that the mouse is over. However, I would have thought that the drag events should have worked.Syllabic
I am faced with the same problem. Would like to hear an answer. Will checkout drag events.Mycetozoan
R
4

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)
Raker answered 21/3, 2012 at 13:49 Comment(0)
I
0

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.

Improbable answered 16/4, 2011 at 21:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.