0

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.

Louis van Tonder
  • 3,664
  • 3
  • 31
  • 62
Ivory244
  • 975
  • 1
  • 8
  • 12

1 Answers1

0

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.

Community
  • 1
  • 1
sls
  • 284
  • 1
  • 7
  • Thank you for your response. I set okbutton->setDefault(true) and changed connect(lineEdit, SIGNAL(returnPressed()), okButton, SIGNAL(clicked())); to connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(chkPassword())); – Ivory244 Jul 29 '14 at 19:01
  • I would like the user to be able to tab to either the lineedit, okbutton, or cancel button and press enter to perform the desired function. If I am understanding correctly, I believe I need to create an event filter for QLineEdit to handle the returnPressed() instead of connect statement above. Although I am still new to qt and not sure how to set up the event filter for the line edit to only handle returnPressed signal and not allow the default button logic to handle it as well. Any suggestions you can provide would be greatly appreciated. Thank you. – Ivory244 Jul 29 '14 at 19:22
  • @Ivory244 don't change the `connect()`, but rather completely remove it. As long as your `okButton` is set as default, pressing Enter with the `QLineEdit` in focus should have the same effect as clicking on OK, because the `lineEdit` would just ignore the keypress and give it to its parent widget for processing, which should activate the default button. – sls Jul 30 '14 at 10:45
  • @Ivory244 Glad I could help :) Qt is quite tricky in some regards, but very well documented and generally a pleasure to work with. Please accept my answer if it solved your problem. – sls Jul 30 '14 at 12:39