You can use QApplication::focusChanged
along with QTimer::singleShot
to select all the text on a changed focus.
Normally your QApplication
is declared in main, so use QObject::connect
there, eg:
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
My_Main_Window w();
QObject::connect(&a, SIGNAL(focusChanged(QWidget*, QWidget*)),
&w, SLOT(my_focus_changed_slot(QWidget*, QWidget*)));
w.show();
return a.exec();
}
Remember to make the public slot in the My_Main_Window
class:
public slots:
void my_focus_changed_slot(QWidget*, QWidget*);
Then in your definition of my_focus_changed_slot
, check if the second QWidget*
(which points to the newly focused widget) is the same as the QLineEdit
you wish to select all of, and do so using QTimer::singleShot
so that the event loop gets called, then the QLineEdit
has the selectAll
slot called immediately after, eg
void My_Main_Window::focus_changed(QWidget*, QWidget* new_widget) {
if (new_widget == my_lineedit) {
QTimer::singleShot(0, my_lineedit, &QLineEdit::selectAll);
}
}
where my_lineedit
is a pointer to a QLineEdit
and is part of the My_Main_Window
class.