我如何在小部件窗口之外接收或处理 mouseMoveEvent(s)?
我正在编写 Qt 应用程序,每当鼠标移动到屏幕中的某个区域时,它就会播放淡入动画,而每当鼠标移出同一区域时,就会播放淡出动画。
我已经在堆栈溢出中找到了类似的问题,但是,它并不能完全回答我的问题。 (此处有类似问题)
如果我在应用程序中安装事件过滤器,我是否能够查看应用程序中的所有事件,即使它位于我的小部件窗口之外?
如果没有,我知道有一种替代方案,涉及在 LeaveEvent() 的重新实现中使用 QWidget::grabMouse() 。但如果我这样做,我是否能够与应用程序之外的任何内容进行交互?
编辑:虽然我使用 Qt 库,但我的应用程序仅用于部署到 Windows。
I am writing Qt application which plays a fade-in animation whenever the mouse is moved to a certain area in the screen, and a fade out animation whenever the mouse is moved out of that same area.
I've already found a similar question here in stack overflow, however, it does not quite answer my question. (similar question here)
If I install an event filter to the application, will I be able to see ALL the events in the application even if it's outside my widget window?
If not, I am aware of an alternative involving QWidget::grabMouse() inside a reimplementation of leaveEvent(). But if I do so, will I be able to interact with anything OUTSIDE my application?
edit: though i am using the Qt library, my application is only for deployment to Windows.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相当肯定答案是否定的,因为小部件之外的事件由操作系统窗口管理器处理(并传播到该空间中的任何应用程序)。
但是,您可以通过调用 QCursor::pos() 来获取屏幕上任何位置的鼠标位置,您可以定期轮询以找出鼠标所在的位置。
I'm fairly the certain the answer is no, because events outside of your widgets are handled by the OSs window manager (and propagated onto whatever application is in that space).
However you can get the mouse position anywhere on the screen by calling
QCursor::pos()
, you could poll at regular intervals to find out where the mouse is.您可以尝试使用
Qt::WindowStaysOnTopHint
、Qt::FramelessWindowHint
和Qt::FramelessWindowHint
创建一个完全透明的窗口,使其位于要接收鼠标事件的区域顶部。 code>Qt::ToolTip 标志(最后一个可能会阻止窗口接收焦点)、Qt::WA_TranslucentBackground
属性和启用的鼠标跟踪。You could try creating a completely transparent window that stays on top of the area where you want to receive mouse events, with the
Qt::WindowStaysOnTopHint
,Qt::FramelessWindowHint
andQt::ToolTip
flags (the last one might prevent the window from receiving the focus), theQt::WA_TranslucentBackground
attribute and the mouse tracking enabled.如果您使用的是 Windows,则可以创建 全局钩子来接收每条鼠标消息(就在它发送到鼠标指针下的窗口之前)。不幸的是我不知道其他操作系统中是否存在此功能。
If you are on Windows, you can create a global hook to receive every mouse message (right before it's sent to the window under the mouse pointer). Unfortunately I don't know whether this functionality exists in other OSs.