Question

I am new to QT GUI programming and I am using QT Creator. The problem I am seeing is with my pushbutton and line edit when the user presses the enter key. My program opens a groupbox and allows the user to enter a password and press ok or cancel.

If the user presses the enter key when the line edit has focus, the program seems to emit a second signal for QAbstractButton animateClick(); So when the next group box opens and prompts the user to press ok or cancel, the program continues as if the user pressed the ok button.

I set up my Push buttons to allow the user to press the tab key and hit the enter key. In order to obtain this functionality I set okbutton->setAutodefault(true); and cancelButton->setAutodefault(true);

I also have a lineEdit for the user to enter a password and press enter. I set this up by connecting the return pressed signal to the ok button shown below.

connect(lineEdit, SIGNAL(returnPressed()), okButton, SIGNAL(clicked()));

I also tried to connect return pressed signal directly to the slot but the problem still occurs.

connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(chkPassword()));

If the user clicks the okButton with the mouse or I set okButton->setAutoDefault(false); the program functions as it should. I would like to know if it is possible to disable the animateClick signal that occurs when the line edit has focus.

Any help with this would be greatly appreciated.

Was it helpful?

Solution

Try never connecting lineEdit with the okButton, i.e. comment out this line in your code:

connect(lineEdit, SIGNAL(returnPressed()), okButton, SIGNAL(clicked()));

and check if the program behaves as you want it to.


Also, you might want to call

okButton->setDefault(true);

in addition to your setAutoDefault() calls on both buttons, depending on what exactly you want to happen when the user presses Enter with various things in focus. See this answer for more information on this subject.


I assume, since you never mention it, that you do not call setDefault() for any of your buttons. Thus, okButton is your first button with the autoDefault property set to true. This means that okButton becomes the dialog's "button of choice" whenever the dialog has to handle an Enter keypress that did not happen on some other autoDefault button (such as cancelButton in your case).

Everytime any widget in your dialog receives an Enter keypress and does not consume the event itself, the event ends up being handled by the dialog's default-button handling logic, which results in a click on the aforementioned "button of choice".

QLineEdit is not used for multi-line editing, so it probably just does not consume the key press event for Enter (while still handling it, in that it sends outs the returnPressed() signal). Thus, your connection of returnPressed() ends up doing whatever it is doing, and, additionaly, the default-button logic ends up clicking your button a second time.

Also check out this thread on the Qt project forums which seems to solve the same problem differently.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top