在QT中查找当前鼠标位置 [英] Finding current mouse position in QT

查看:241
本文介绍了在QT中查找当前鼠标位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次尝试写一个QT应用程序,我只是想了解它的工作原理。我的目标是有一个400x400的小部件,知道鼠标的确切位置,当鼠标悬停在它。例如,如果鼠标悬停在左上角,则位置可能为10,10(或类似的)。如果鼠标在右下角,它可能说390,390。

This is my first attempt at writing a QT app, and I'm just trying to get a feel for how it works. My goal is to have a 400x400 widget which knows the exact position of the mouse when the mouse is hovering over it. For example, if the mouse was hovering in the top left corner, the position might be 10,10 (or something similar). If the mouse is in the bottom right corner, it might say 390,390.

最终,这些坐标将显示在主窗口的标签中,但这应该是微不足道的。我坚持坐标的实际获取。任何想法?

Eventually, these coordinates will be displayed in a label on the main window, but that should be trivial. I'm stuck at the actual fetching of the coordinates. Any ideas?

推荐答案

对于您的小部件,您必须启用鼠标跟踪。

For your widget, you must enable mouse tracking.

然后,您可以安装事件过滤器,注意鼠标事件并寻找移动事件,也可以继承QWidget并覆盖鼠标事件,寻找鼠标移动事件。

Then, you can either install an event filter, paying attention to mouse events and looking for the move event, or you can inherit from QWidget and override the mouse event, looking for mouse move events.

http://doc.qt.io /qt-4.8/qwidget.html#mouseTracking-prop

http://doc.qt.io/qt-4.8/eventsandfilters.html

http://doc.qt.io/qt-4.8/qmouseevent.html

这篇关于在QT中查找当前鼠标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆