如何在 Qt 中检测用户不活动?
如何检测 Qt QMainWindow 中的用户不活动状态?到目前为止,我的想法是有一个 QTimer 来递增计数器,如果传递了某个值,则锁定应用程序。任何鼠标或按键交互都应将计时器设置回 0。但是我需要知道如何正确处理重置的输入事件;我可以重新实现:
virtual void keyPressEvent(QKeyEvent *event)
virtual void keyReleaseEvent(QKeyEvent *event)
virtual void mouseDoubleClickEvent(QMouseEvent *event)
virtual void mouseMoveEvent(QMouseEvent *event)
virtual void mousePressEvent(QMouseEvent *event)
virtual void mouseReleaseEvent(QMouseEvent *event)
...但是 QMainWindow 中所有小部件的事件处理程序不会阻止这些控件中发生的事件到达 QMainWindow 吗?是否有更好的架构来检测用户活动?
How can I detect user inactivity in a Qt QMainWindow? My idea so far is to have a QTimer that increments a counter, which, if a certain value is passed, locks the application. Any mouse or key interaction should set the timer back to 0. However I need to know how to properly handle input events which reset; I can re-implement:
virtual void keyPressEvent(QKeyEvent *event)
virtual void keyReleaseEvent(QKeyEvent *event)
virtual void mouseDoubleClickEvent(QMouseEvent *event)
virtual void mouseMoveEvent(QMouseEvent *event)
virtual void mousePressEvent(QMouseEvent *event)
virtual void mouseReleaseEvent(QMouseEvent *event)
...but won't the event handlers of all the widgets in the QMainWindow prevent events occurring in those controls from reaching the QMainWindow's? Is there a better architecture for detecting user activity as it is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用自定义事件过滤器来处理应用程序接收到的所有键盘和鼠标事件,然后再将其传递给子窗口小部件。
然后使用类似的东西
这肯定有效(我自己尝试过)。
编辑:非常感谢 ereOn 指出我之前的解决方案不是很有用。
You could use a custom event filter to process all keyboard and mouse events received by your application before they are passed on to the child widgets.
Then use something like
This definitely works (I've tried it myself).
EDIT: And many thanks to ereOn for pointing out that my earlier solution was not very useful.
更好的方法之一是捕获 xidle 信号,而不是捕获来自用户的大量事件。这里还需要捕获 QEvent:MouseMove 事件
One of better approach will be to catch xidle signal rather then catching so many events from user. Here one need to capture QEvent:MouseMove event also
最干净的方法是重写主窗口小部件的
eventFilter
函数并将其设置为应用程序对象上的事件过滤器。在过滤器内,您可以使用
dynamic_cast
来检查事件是否是QInputEvent
。所有与用户交互的事件均源自QInputEvent
,并以这种方式进行识别。您可以将基类
QWidget
替换为从QWidget
派生的任何类。 (包括QMainWindow
。)请注意,event
函数必须将事件传递给基类,并返回其返回值。如果您有多个窗口,您可能还需要检查事件目标对象是否是您的窗口或其
QWidget
子窗口之一。The cleanest way is to override the
eventFilter
function of your main window widget and set it as event filter on your application object.Inside the filter you can use
dynamic_cast
to check if the event is aQInputEvent
. All events with user interaction are derived fromQInputEvent
and are recognized this way.You can replace the base class
QWidget
with any class derived fromQWidget
. (IncludingQMainWindow
.) Note that theevent
function must pass the event to the base class, and return its return value.If you have more then one window, you might also want to check, that the event target object is your window or one of its
QWidget
children.