没有 MousePress 的 PyQt4 MouseMove 事件
我需要捕捉用户何时将鼠标移动到 GUI 上,而不是当他们按住鼠标按钮时(这会做不同的事情)。
我找不到任何方便的方法来做到这一点, 除了定期查找鼠标位置并检查其之前的位置... 这会很糟糕。
mouseMoveEvent 仅当鼠标移动且按下鼠标左键时才会被调用, 当然,除非小部件具有“鼠标跟踪”功能。鼠标跟踪对我来说不是一个选项,因为当移动鼠标并按下鼠标左键时,GUI 的行为必须有所不同。
有没有内置的方法可以做到这一点? (或者只是任何聪明的想法?)
例如: 有没有办法随时检查鼠标左键是否被按下?
或者可以应用于 QRect(坐标)的“鼠标悬停”事件?
非常感谢。
Windows 7 (32)
蟒蛇2.7
PyQt4
I need to catch when a User moves the mouse over the GUI, but not when they're holding down the mouse button (which would do something different).
I can't find any conveniant method to do this,
except to periodically find the mouse position and check it to it's previous position...
Which would suck.
The mouseMoveEvent is only called when the mouse is moved whilst the left mouse button is pressed,
unless ofcourse the widget has 'mouse tracking'. Mouse tracking is not an option for me, because the GUI must behave differently when the mouse is moved and the left mouse button is pressed.
Are there any inbuilt methods to do this?
(or just any clever ideas?)
eg:
Is there a way to check if the left mouse button is being pressed at any time?
Or a 'mouse hover' event that can be applied to a QRect (coordinates)?
Muchas gracias.
Windows 7 (32)
python 2.7
PyQt4
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最直接的方法是在 qApp 上安装事件过滤器:
The most straightforward way to do this is to install an event filter on qApp:
正如人们所说,正确的方法似乎是在小部件上调用 setMouseTracking(True) 。我想补充的是,完成此操作后,您可以区分鼠标移动和鼠标拖动,如下所示:
As people have said, the correct approach seems to be to call setMouseTracking(True) on the widget. What I'd like to add is that, having done this, you can distinguish between a mouse motion and a mouse drag as follows:
首先调用 setMouseTracking(True) 方法。然后 mouseMoveEvent 将在没有按下任何按钮的情况下被触发。
call setMouseTracking(True) method first. Then mouseMoveEvent will be fired without any button pressed.
您似乎误解了
mouseTracking
做。它只会导致mouseMoveEvent
被触发,不会发生任何其他事情。换句话说,这正是您所需要的。检查事件的 buttons() 以查看是否按下了任何按钮:
It seems you've misunderstood what
mouseTracking
does. It only causesmouseMoveEvent
to be fired, nothing else. In other words, it's exactly what you need.Check the event's buttons() to see if any button was pressed: