Qt:如何检测是否选择了小部件? [英] Qt: how to detect whether a widget is selected?

查看:44
本文介绍了Qt:如何检测是否选择了小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有看到任何信号/插槽/功能可以告诉我是否通过鼠标选择了小部件? 是否可以使用这样的功能来告诉我是否选择了当前的QWidget?以及如何区分当前小部件已选择"和其子小部件之一已选择"?

I didn't see any signal/slot/function that could tell me whether a widget is selected by mouse? Is it possible to have such an function to tell me whether the current QWidget is selected? And How could I differentiate between "the current widget is selected" and "one of its child widget is selected?"

推荐答案

您可以使用hasFocus()函数检查对小部件的关注. focus属性保存窗口小部件是否具有键盘输入焦点.您还可以使用QApplication::focusWidget()获取具有焦点的应用程序的当前小部件.您可以获取指向焦点小部件的指针,例如:

You can check focus on a widget using hasFocus() function. focus property holds whether the widget has keyboard input focus or not. You can also get the current widget of the application that has the focus using QApplication::focusWidget(). You can get a pointer to the focused widget like:

QWidget * fw = qApp->focusWidget();

更改焦点小部件时,会发出QApplication::focusChanged(QWidget *old, QWidget *now)信号.您可以将其连接到插槽,根据焦点更改,您可以根据自己的喜好进行操作.

When the focused widget is changed QApplication::focusChanged(QWidget *old, QWidget *now) signal is emitted.You can connect it to a slot in which you do what ever you like based on the focus change.

这篇关于Qt:如何检测是否选择了小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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