Qt: how to detect whether a widget is selected?(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:如何检测一个小部件是否被选中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Qt:如何检测一个小部件是否被选中?
基础教程推荐
- 我有静态或动态 boost 库吗? 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 这个宏可以转换成函数吗? 2022-01-01
