Search code examples
pyqt5signalsexit-codeqdialog

How to generate accept signal for dialog.exec in PyQt5


In main.py

returnCode = self.rouDialogForm.exec_()
if returnCode == QtWidgets.QDialog.Accepted:
    print(float(self.rouDialogForm.ui.leStartMhz.text()))

if returnCode ==QtWidgets.QDialog.Rejected:
    print(float(self.rouDialogForm.ui.leStopMhz.text()))

In rouDialog.py

def setupUi(self, Dialog):
    #GUI CODE
    self.retranslateUi(Dialog)
    QtCore.QMetaObject.connectSlotsByName(Dialog)
    self.butConnect.clicked.connect(self.acceptDialog)

def acceptDialog(self):
    self.accept()

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        #Label set texts are here
        import myResources_rc

I can catch the Rejected signal that is generated after clicking the window closing icon and print out the required text from the line edit. But when I press the button that will generate accept signal(but connect) program just crashes run time.

I tried different syntax and different imports to make it work.

attempt #1: in rouDialog.py instead of self.accept()

self.done(QtWidgets.QDialog.Accepted)

another attempt: in rouDialog.py instead of self.accept()

super(Ui_Dialog,self).accept()

another one:

QtWidgets.QDialog.accept(self)

Solution

  • SOLVED: Adding this to my rouDialog.py solved all my problems.Hope it will help someone else in future.

        self.butReadout.clicked.connect(Dialog.accept)
    
        @QtCore.pyqtSlot()
        def accept(self):
            pass
    

    Edit: I can't mark my answer as correct , since i need to wait 2 days.