Detect when mouse enter QGraphicsItem with button down

怎甘沉沦 提交于 2019-11-30 15:50:31

问题


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?


回答1:


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)



回答2:


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.



来源:https://stackoverflow.com/questions/3845615/detect-when-mouse-enter-qgraphicsitem-with-button-down

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!