How to set the widget has the default focus properties
-
wrote on 30 Nov 2017, 08:53 last edited by
Probably what you need is setFocusProxy.
If that doesn't work you can set the focus "manually" after the widget has been shown.
-
wrote on 30 Nov 2017, 09:37 last edited by
-
wrote on 30 Nov 2017, 10:21 last edited by Qt_crazyer
I have used this function. But it doesn't work.
study::study(QWidget *parent) : QMainWindow(parent) { widget = new QWidget; l1 = new QLineEdit; l2 = new QLineEdit; m1 = new QLabel(tr("Label1")); m2 = new QLabel(tr("Lable2")); lay = new QHBoxLayout(widget); lay->addWidget(m1); lay->addWidget(l1); lay->addWidget(m2); lay->addWidget(l2); lay->setSpacing(10); lay->setMargin(10); l2->setFocus(); this->setCentralWidget(widget); }
Should this function be used here?
-
I have used this function. But it doesn't work.
study::study(QWidget *parent) : QMainWindow(parent) { widget = new QWidget; l1 = new QLineEdit; l2 = new QLineEdit; m1 = new QLabel(tr("Label1")); m2 = new QLabel(tr("Lable2")); lay = new QHBoxLayout(widget); lay->addWidget(m1); lay->addWidget(l1); lay->addWidget(m2); lay->addWidget(l2); lay->setSpacing(10); lay->setMargin(10); l2->setFocus(); this->setCentralWidget(widget); }
Should this function be used here?
wrote on 30 Nov 2017, 10:35 last edited by Taz742@Qt_crazyer
Please try l2->setFocus(Qt::otherFocusReason); -
@Qt_crazyer
Please try l2->setFocus(Qt::otherFocusReason);wrote on 30 Nov 2017, 10:42 last edited by Qt_crazyer@Taz742
Yes, I have also tried this function, but it doesn't work. This is just a sample program, it is very simple. I do not understand why?...
-
@Taz742
Yes, I have also tried this function, but it doesn't work. This is just a sample program, it is very simple. I do not understand why?...
wrote on 30 Nov 2017, 10:45 last edited by@Qt_crazyer
An other solution is to use a singleShot timer :QTimer::singleShot(0,lineEdit,SLOT(setFocus()));
The focus will then be set once the application is free.
See solution follow the link..
-
I have used this function. But it doesn't work.
study::study(QWidget *parent) : QMainWindow(parent) { widget = new QWidget; l1 = new QLineEdit; l2 = new QLineEdit; m1 = new QLabel(tr("Label1")); m2 = new QLabel(tr("Lable2")); lay = new QHBoxLayout(widget); lay->addWidget(m1); lay->addWidget(l1); lay->addWidget(m2); lay->addWidget(l2); lay->setSpacing(10); lay->setMargin(10); l2->setFocus(); this->setCentralWidget(widget); }
Should this function be used here?
wrote on 30 Nov 2017, 10:54 last edited by@Qt_crazyer I am assuming l2 is a member (using "m" as prefix for members would help).
Just set the focus with a delay, in your case:
QTimer::singleShot(0, this, [this] { l2->setFocus(); });
-
@Qt_crazyer
An other solution is to use a singleShot timer :QTimer::singleShot(0,lineEdit,SLOT(setFocus()));
The focus will then be set once the application is free.
See solution follow the link..
wrote on 30 Nov 2017, 10:56 last edited by@Taz742
Thank you so much. The problem has been solved. Thank you for your patient guidance. -
@Qt_crazyer I am assuming l2 is a member (using "m" as prefix for members would help).
Just set the focus with a delay, in your case:
QTimer::singleShot(0, this, [this] { l2->setFocus(); });
wrote on 30 Nov 2017, 10:58 last edited by@vivaladav
Thank you for your reply. This method is really effective. -
@vivaladav
Thank you for your reply. This method is really effective.wrote on 30 Nov 2017, 11:04 last edited by Taz742@Qt_crazyer
Nope. Do not forget mark this topic as SOLVED. -
wrote on 30 Nov 2017, 12:05 last edited by
What about this ?
setTabOrder(l2,l1);
-
wrote on 30 Nov 2017, 12:13 last edited by
@mpergand
Thank you for your advice. I have tried this function. This is indeed an effective way. -
wrote on 30 Nov 2017, 14:08 last edited by
Keep in mind that setTabOrder might not be the right solution if you introduce more widgets (as in any normal working application).
11/14