PDA

View Full Version : Maintain QLineEdit Focus



Chris2oph
11th July 2016, 13:41
Good Day, I am having trouble maintaining the position of the cursor on a simple login application.

A user selects to login using my interface, this opens up a QDialog window which contains two QLineEdits (username and password), and two QPushButtons (OK and cancel). The very first time this window is displayed, the cursor is present on the username line. However after logging in (either successfully or unsuccessfully) and closing the login window, upon returning to the login window the cursor will now be in position on the password line. I have verified that the cursor position upon re-entering the login window will simply be the last position that the cursor was in upon leaving the login window.

I need for the cursor to be on the username line every time this window is opened and I am having trouble getting this to occur. I feel that this will be an extremely simple thing to do, but the QT documentation on the web is not proving very useful for this.

Any help would be greatly appreciated :-)

ChrisW67
12th July 2016, 10:51
Call QWidget::setFocus() on the user name widget. You could reimplement the dialog showEvent() and call the function there after calling the base implementation.

Alternatively, in the slots that handle the two buttons set the focus before hiding the dialog.

Chris2oph
12th July 2016, 11:05
Many thanks for your suggestion, this has worked very nicely! :-)