Unsolved QLineEdit::returnPressed closes dialog
-
Hi, I have a dialog with a line edit controls.
By default the dialog ok button has the focus which is fine.
My problem is, that if the return key is pressed while editing the line edit the dialog closes.
What is the correct way to fix that? -
Hi
Did you use your own QPushButton for Ok/Cancel or did you use the QDialogButtonBox ? -
I am using QDialogButtonBox
-
I tried listening to editingFinished and set the focus to the line edit, but it didn't help
-
Turn off the OK button as the default button.
From the doc:If you want a specific button to be default you need to call QPushButton::setDefault() on it yourself. However, if there is no default button set and to preserve which button is the default button across platforms when using the QPushButton::autoDefault property, the first push button with the accept role is made the default button when the QDialogButtonBox is shown,
-
Hi
As @mpergand says, QDialogButtonBox will help you and find a new default button even if you
say there is no default. Also if you set Cancel as default, it will still close the dialog.
I cant promise its the best way to solve it but overriding keyPressEvent worked fine
for me and also gave me the option to make enter/return into tab so pressing return would move
to next lineEdit. (not shown)class Dialog : public QDialog { Q_OBJECT ..... protected: virtual void keyPressEvent(QKeyEvent *event) override { int key = event->key(); if (key == Qt::Key_Return || key == Qt::Key_Enter) event->ignore(); else QDialog::keyPressEvent(event); } };
-
Depending on what you're really trying to achieve, there are a few ways I can think of prevent the enter/return keys from closing your dialog, assuming you want to keep the QDialogButtonBox to enjoy the button layouts functionality it gives you by following the style guidelines across platforms. The solution provided by mrjj will most likely achieve this, but it will also prevent any of those specified key presses from reaching the dialog, and depending on the other functionality found in the dialog, this may or may not be acceptable. One alternative which allows you to maintain the key press events is to override your dialog's show event so as to allow the QDialogButtonBox to be shown, after which it will have set a default button with an AcceptRole, and to then set all buttons to not be defaults.
void MyDialog::showEvent(QShowEvent* event) { // When a QDialogButtonBox is shown, it will set a default button if none are found so we need to disable the // default buttons after the button box has been shown. QDialog::showEvent(event); // For example, with a dialog which has two buttons, Save and Cancel, we remove all defaults // It might be good enough to remove the default on just the buttons with have the AcceptRole, but // I didn't test extensively enough to see what would happen if any buttons had "autoDefault" set or // verify this behavior on all platforms. ui->buttonBox->button(QDialogButtonBox::Save)->setDefault(false); ui->buttonBox->button(QDialogButtonBox::Cancel)->setDefault(false); }
If the default button behavior is still desired but not when a specific widget has focus, then the most flexible option would be to create an event filter in your dialog such that you find the right set of conditions required to either propagate the key press event or to your dialog or filter it out.