'Connected function runs twice and exits [duplicate]

I'm frustrated when trying to figure out how to connect correctly editingFinished with a function.

Couldn't make the code shorter, because every time I did, it worked, but I need the other parts. Anyone has a clue?


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(991, 695)
        self.gridLayout_4 = QtWidgets.QGridLayout(Dialog)
        self.gridLayout_4.setObjectName("gridLayout_4")
        self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
        self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
        self.buttonBox.setObjectName("buttonBox")
        self.gridLayout_4.addWidget(self.buttonBox, 2, 0, 1, 1)
        self.tabWidget = QtWidgets.QTabWidget(Dialog)
        self.tabWidget.setObjectName("tabWidget")
        self.regular_file1 = QtWidgets.QWidget()
        self.regular_file1.setObjectName("regular_file1")
        self.gridLayout_2 = QtWidgets.QGridLayout(self.regular_file1)
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_3.setObjectName("horizontalLayout_3")
        self.label_2 = QtWidgets.QLabel(self.regular_file1)
        self.label_2.setObjectName("label_2")
        self.horizontalLayout_3.addWidget(self.label_2)
        self.label = QtWidgets.QLabel(self.regular_file1)
        self.label.setObjectName("label")
        self.horizontalLayout_3.addWidget(self.label)
        self.gridLayout_2.addLayout(self.horizontalLayout_3, 1, 0, 1, 1)
        self.verticalScrollBar_2 = QtWidgets.QScrollBar(self.regular_file1)
        self.verticalScrollBar_2.setOrientation(QtCore.Qt.Vertical)
        self.verticalScrollBar_2.setObjectName("verticalScrollBar_2")
        self.gridLayout_2.addWidget(self.verticalScrollBar_2, 3, 1, 1, 1)
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.listWidget = QtWidgets.QListWidget(self.regular_file1)
        self.listWidget.setObjectName("listWidget")
        self.horizontalLayout.addWidget(self.listWidget)
        self.verticalScrollBar = QtWidgets.QScrollBar(self.regular_file1)
        self.verticalScrollBar.setOrientation(QtCore.Qt.Vertical)
        self.verticalScrollBar.setObjectName("verticalScrollBar")
        self.horizontalLayout.addWidget(self.verticalScrollBar)
        self.listView = QtWidgets.QListView(self.regular_file1)
        self.listView.setObjectName("listView")
        self.horizontalLayout.addWidget(self.listView)
        self.gridLayout_2.addLayout(self.horizontalLayout, 3, 0, 1, 1)
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.pushButton_4 = QtWidgets.QPushButton(self.regular_file1)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.pushButton_4.sizePolicy().hasHeightForWidth())
        self.pushButton_4.setSizePolicy(sizePolicy)
        self.pushButton_4.setIconSize(QtCore.QSize(34, 16))
        self.pushButton_4.setObjectName("pushButton_4")
        self.horizontalLayout_2.addWidget(self.pushButton_4)
        self.pushButton_5 = QtWidgets.QPushButton(self.regular_file1)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.pushButton_5.sizePolicy().hasHeightForWidth())
        self.pushButton_5.setSizePolicy(sizePolicy)
        self.pushButton_5.setIconSize(QtCore.QSize(34, 16))
        self.pushButton_5.setObjectName("pushButton_5")
        self.horizontalLayout_2.addWidget(self.pushButton_5)
        spacerItem = QtWidgets.QSpacerItem(388, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout_2.addItem(spacerItem)
        self.pushButton_3 = QtWidgets.QPushButton(self.regular_file1)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.pushButton_3.sizePolicy().hasHeightForWidth())
        self.pushButton_3.setSizePolicy(sizePolicy)
        self.pushButton_3.setIconSize(QtCore.QSize(34, 16))
        self.pushButton_3.setObjectName("pushButton_3")
        self.horizontalLayout_2.addWidget(self.pushButton_3)
        self.gridLayout_2.addLayout(self.horizontalLayout_2, 4, 0, 1, 1)
        self.lineEdit = QtWidgets.QLineEdit(self.regular_file1)
        self.lineEdit.setObjectName("lineEdit")

        #### IMPORTANT PART ####
        a = "MY NAME"
        self.lineEdit.editingFinished.connect(lambda x=a: self.search(x))


        self.gridLayout_2.addWidget(self.lineEdit, 0, 0, 1, 1)
        self.tabWidget.addTab(self.regular_file1, "")
        self.yaml_file2 = QtWidgets.QWidget()
        self.yaml_file2.setObjectName("yaml_file2")
        self.gridLayout = QtWidgets.QGridLayout(self.yaml_file2)
        self.gridLayout.setObjectName("gridLayout")
        self.gridLayout_5 = QtWidgets.QGridLayout()
        self.gridLayout_5.setObjectName("gridLayout_5")
        self.tabWidget_2 = QtWidgets.QTabWidget(self.yaml_file2)
        self.tabWidget_2.setObjectName("tabWidget_2")
        self.only_ref_tab = QtWidgets.QWidget()
        self.only_ref_tab.setObjectName("only_ref_tab")
        self.horizontalLayout_6 = QtWidgets.QHBoxLayout(self.only_ref_tab)
        self.horizontalLayout_6.setObjectName("horizontalLayout_6")
        self.listWidget_2 = QtWidgets.QListWidget(self.only_ref_tab)
        self.listWidget_2.setObjectName("listWidget_2")
        self.horizontalLayout_6.addWidget(self.listWidget_2)
        self.tabWidget_2.addTab(self.only_ref_tab, "")
        self.only_dev_tab = QtWidgets.QWidget()
        self.only_dev_tab.setObjectName("only_dev_tab")
        self.horizontalLayout_5 = QtWidgets.QHBoxLayout(self.only_dev_tab)
        self.horizontalLayout_5.setObjectName("horizontalLayout_5")
        self.listWidget_3 = QtWidgets.QListWidget(self.only_dev_tab)
        self.listWidget_3.setObjectName("listWidget_3")
        self.horizontalLayout_5.addWidget(self.listWidget_3)
        self.tabWidget_2.addTab(self.only_dev_tab, "")
        self.modified_tab = QtWidgets.QWidget()
        self.modified_tab.setObjectName("modified_tab")
        self.gridLayout_3 = QtWidgets.QGridLayout(self.modified_tab)
        self.gridLayout_3.setObjectName("gridLayout_3")
        self.listWidget_4 = QtWidgets.QListWidget(self.modified_tab)
        self.listWidget_4.setObjectName("listWidget_4")
        self.gridLayout_3.addWidget(self.listWidget_4, 0, 0, 1, 1)
        self.tabWidget_2.addTab(self.modified_tab, "")
        self.gridLayout_5.addWidget(self.tabWidget_2, 0, 0, 1, 1)
        self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_4.setObjectName("horizontalLayout_4")
        spacerItem1 = QtWidgets.QSpacerItem(388, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout_4.addItem(spacerItem1)
        self.pushButton_2 = QtWidgets.QPushButton(self.yaml_file2)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.pushButton_2.sizePolicy().hasHeightForWidth())
        self.pushButton_2.setSizePolicy(sizePolicy)
        self.pushButton_2.setIconSize(QtCore.QSize(34, 16))
        self.pushButton_2.setObjectName("pushButton_2")
        self.horizontalLayout_4.addWidget(self.pushButton_2)
        self.gridLayout_5.addLayout(self.horizontalLayout_4, 1, 0, 1, 1)
        self.gridLayout.addLayout(self.gridLayout_5, 0, 0, 1, 1)
        self.tabWidget.addTab(self.yaml_file2, "")
        self.gridLayout_4.addWidget(self.tabWidget, 0, 0, 1, 1)

        self.retranslateUi(Dialog)
        self.tabWidget.setCurrentIndex(0)
        self.tabWidget_2.setCurrentIndex(2)
        self.buttonBox.accepted.connect(Dialog.accept) # type: ignore
        self.buttonBox.rejected.connect(Dialog.reject) # type: ignore
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.label_2.setText(_translate("Dialog", "TextLabel"))
        self.label.setText(_translate("Dialog", "TextLabel"))
        self.pushButton_4.setText(_translate("Dialog", "next diff"))
        self.pushButton_5.setText(_translate("Dialog", "prev diff"))
        self.pushButton_3.setText(_translate("Dialog", "Open with editor"))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.regular_file1), _translate("Dialog", "regular_file"))
        self.tabWidget_2.setTabText(self.tabWidget_2.indexOf(self.only_ref_tab), _translate("Dialog", "only_ref"))
        self.tabWidget_2.setTabText(self.tabWidget_2.indexOf(self.only_dev_tab), _translate("Dialog", "only_dev"))
        self.tabWidget_2.setTabText(self.tabWidget_2.indexOf(self.modified_tab), _translate("Dialog", "Modified"))
        self.pushButton_2.setText(_translate("Dialog", "Open with editor"))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.yaml_file2), _translate("Dialog", "yaml_file"))


    ### IMPORTANT PART ###
    def search(self, a):
        print(f"hello world {a}")

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

Desired output:

hello world MY_NAME

The REAL output:

hello world MY NAME
hello world MY NAME

and the program crashes with no explanation of the problem...

Any ideas?

*** EDIT *** The trigger is when editing the line and pressing "Enter".

When commenting out these lines:

self.buttonBox.accepted.connect(Dialog.accept) # type: ignore
self.buttonBox.rejected.connect(Dialog.reject) # type: ignore

It works... I don't understand... :(

SOLUTION

I have no idea why anyone would think that this question is duplicated... it has nothing to do with the link attached to it.

So for anyone else who would arrive here: The answer is that when I pressed Enter, the OK button was triggered as well. You could see a blue mark surrounding it, that means that when you press the Enter, then he thinks that he's been pressed as well and that explains why the program exits. That explains why when I commented out the "okButton.connected", it worked. Solution is for me to put a new button that would act like an Enter button, but you could disable the OK button as well.



Solution 1:[1]

The documentation is clear:

void QLineEdit::editingFinished()

This signal is emitted when the Return or Enter key is pressed or the line edit loses focus. Note that if there is a validator() or inputMask() set on the line edit and enter/return is pressed, the editingFinished() signal will only be emitted if the input follows the inputMask() and the validator() returns QValidator::Acceptable.

(emphasis mine)

In your case the following happens:

  • When you press enter the signal editingFinished is emitted.
  • As it is an enter, the accepted signal of the QDialog is also activated, which closes the window and that makes the QLinEdit lose focus, so the editingFinished signal is emitted again.

You have several options:

  • Don't use QDialog but a simple QWidget if you can.
  • Print if the QLineEdit has the focus: if self.lineEdit.hasFocus(): print(f"hello world {a}").

Note: The program does not break but a QDialog has the default behavior when enter is pressed.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 eyllanesc