Qt: how to detect whether a widget is selected?

牧云@^-^@ 提交于 2019-12-10 04:35:42

问题


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?"


回答1:


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();

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.



来源:https://stackoverflow.com/questions/28807064/qt-how-to-detect-whether-a-widget-is-selected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!