如何检测一个小部件是否被选中

如何检测一个小部件是否被选中

本文介绍了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:如何检测一个小部件是否被选中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 22:10