If I have two text boxes in my main window how can I check which one is active/being used by the user?
Asked
Active
Viewed 639 times
2
-
1[That][1] should be a good start point to start from... [1]: http://stackoverflow.com/questions/321656/when-a-qt-widget-gets-focus – evilruff Aug 06 '13 at 20:44
2 Answers
3
You can use QApplication::focusWidget() function to see which widget currently has focus.
Or you can use the QWidget::hasFocus() function to see if your text box has focus.
edit_A = new QTextEdit(this);
edit_B = new QTextEdit(this);
.
.
.
void MyClass::someFunction()
{
if(edit_A->hasFocus())
//edit_A is being used
else if(edit_B->hasFocus())
//edit_B is being used
}

thuga
- 12,601
- 42
- 52
0
The previous answer is right but just in case you want to monitor more components you can use
QWidget * QApplication::focusWidget ()
to get widget with focus.
This approach will allow you to make cleaner code if you wanted to have more widgets monitored. Instead of having a ladder of ifs just use some kind of look-up table to choose what action to take.

Caladan
- 1,471
- 11
- 13