Set QLineEdit focus in Qt
Asked Answered
B

4

22

I am having a qt question. I want the QLineEdit widget to have the focus at application startup. Take the following code for example:

#include <QtGui/QApplication>
#include <QtGui/QHBoxLayout>
#include <QtGui/QPushButton>
#include <QtGui/QLineEdit>
#include <QtGui/QFont>


 int main(int argc, char *argv[])
 {
     QApplication app(argc, argv);

     QWidget *window = new QWidget();
     window->setWindowIcon(QIcon("qtest16.ico"));
     window->setWindowTitle("QtTest");

     QHBoxLayout *layout = new QHBoxLayout(window);

     // Add some widgets.
     QLineEdit *line = new QLineEdit();

     QPushButton *hello = new QPushButton(window);
     hello->setText("Select all");
     hello->resize(150, 25);
     hello->setFont(QFont("Droid Sans Mono", 12, QFont::Normal));

     // Add the widgets to the layout.
     layout->addWidget(line);
     layout->addWidget(hello);

     line->setFocus();

     QObject::connect(hello, SIGNAL(clicked()), line, SLOT(selectAll()));
     QObject::connect(line, SIGNAL(returnPressed()), line, SLOT(selectAll()));

     window->show();
     return app.exec();
 }

Why does line->setFocus() sets the focus on the line widget @app startup only if it is placed after laying out the widgets and if used before it's not working?

Burst answered 9/2, 2009 at 0:28 Comment(0)
B
25

Keyboard focus is related to widget tab order, and the default tab order is based on the order in which widgets are constructed. Therefore, creating more widgets changes the keyboard focus. That is why you must make the QWidget::setFocus call last.

I would consider using a sub-class of QWidget for your main window that overrides the showEvent virtual function and then sets keyboard focus to the lineEdit. This will have the effect of always giving the lineEdit focus when the window is shown.

Binding answered 9/2, 2009 at 16:40 Comment(2)
Little correction: tab order is not the order in which the children are constructed, but the order in which they're added to the parent.Fefeal
That is probably true, but I was paraphrasing the Qt documentation taht states: "You can customize the tab order using QWidget::setTabOrder(). (If you don't, Tab generally moves focus in the order of widget construction.)"Binding
S
27

Another trick that might work is by using the singleshot timer:

QTimer::singleShot(0, line, SLOT(setFocus()));

Effectively, this invokes the setFocus() slot of the QLineEdit instance right after the event system is "free" to do so, i.e. sometime after the widget is completely constructed.

Silica answered 7/3, 2009 at 23:8 Comment(1)
this worked like charm, the event system being free added to the advantage of properly focusing the newly(dynamically) created lineedit in mycase. thanksChronological
B
25

Keyboard focus is related to widget tab order, and the default tab order is based on the order in which widgets are constructed. Therefore, creating more widgets changes the keyboard focus. That is why you must make the QWidget::setFocus call last.

I would consider using a sub-class of QWidget for your main window that overrides the showEvent virtual function and then sets keyboard focus to the lineEdit. This will have the effect of always giving the lineEdit focus when the window is shown.

Binding answered 9/2, 2009 at 16:40 Comment(2)
Little correction: tab order is not the order in which the children are constructed, but the order in which they're added to the parent.Fefeal
That is probably true, but I was paraphrasing the Qt documentation taht states: "You can customize the tab order using QWidget::setTabOrder(). (If you don't, Tab generally moves focus in the order of widget construction.)"Binding
C
4

Perhaps this is an update as the last answer was in 2012 and the OP last edited the question in 2014. They way I got this to work was to change the policy and then set the focus.

line->setFocusPolicy(Qt::StrongFocus);
line->setFocus();
Centaurus answered 10/9, 2016 at 19:39 Comment(0)
B
1

In Qt setFocus() is a slot, you can try other overloaded method which takes a Qt::FocusReason parameter like the line shown below:

line->setFocus(Qt::OtherFocusReason);

You can read about focus reason options in the following link:

http://doc.trolltech.com/4.4/qt.html#FocusReason-enum

Brittni answered 9/2, 2009 at 0:44 Comment(2)
Hi milot. Thanks for your answer. The thing is even if I change to what you wrote, it doesn't work. Since it's a widget slot I assume that I can connect a signal to it at application startup. Do you know which signal is that? Thanks again.Burst
It can be any signal which it's signature is matched with the slot's one, so it can be connected to a signal which doesn't take any parameters.Brittni

© 2022 - 2024 — McMap. All rights reserved.