QDialog problem with returnPressed which is used in another segment
-
I'm creating an application and one function launches a QDialog window. in the window, there is a QLineEdit, a QListWidget and a QButtonBox with the buttons "OK" and "Cancel". The goal of the dialog is to add a single entry to the LineEdit, press "Enter / return", which would add the text from the LineEdit to the ListWidget, and clear the LineEdit. Once the user clicks the "OK" button, the entries from the ListWidget will be returned to the main application.
However, I have encountered an issue and seem unable to fix it.
this is a part of the code:self.lineEdit.returnPressed.connect(self.add_to_list) self.buttonBox.accepted.connect(Dialog.accept) # type: ignore self.buttonBox.rejected.connect(Dialog.reject) # type: ignore
add_to_list takes the text from the line, removes unnecessary whitespaces and puts the text into the list, if the text is not an empty string.
My issue is, that for some reason PyQt registers the "returnPressed" event for the buttonbox as well as the lineEdit, so as soon as the first entry gets added to the list, the data gets returned, as the buttonbox was triggered. I have tried disabling this behavior by using
self.buttonBox.accepted.disconnect()
which allows me to press return to add text to the listWidget, but disables my ability to close the Dialog.
How can I set the buttonbox to only be activated by a manual click?
-
@WorldTeacher Try https://doc.qt.io/qt-6/qpushbutton.html#default-prop to set the buttons to non-default (by passing false to setDefault).
-
@jsulm Does this work for the QDialogButtonBox as well? I took a quick look at the documentation and added these lines:
self.buttonBox.button(QtWidgets.QDialogButtonBox.StandardButton.Ok).setDefault(False) self.buttonBox.button(QtWidgets.QDialogButtonBox.StandardButton.Ok).setAutoDefault(False) self.buttonBox.button(QtWidgets.QDialogButtonBox.StandardButton.Cancel).setDefault(False) self.buttonBox.button(QtWidgets.QDialogButtonBox.StandardButton.Cancel).setAutoDefault(False)
but the Dialog is still closed after I press Return
-
Hi,
Can you provide a minimal script that shows this behaviour ?