Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt: how to detect whether a widget is selected?

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

like image 287
Nyaruko Avatar asked Mar 02 '15 10:03

Nyaruko


1 Answers

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.

like image 195
Nejat Avatar answered Sep 23 '22 17:09

Nejat