[SOLVED] closeEvent question



  • Hi Everyone,

    I have a question with the closeEvent method. If I use self.accept() in the close event I get what I am expecting. If I use event.accept() the window did not execute. Why is event.accept() not working? See below for the example.

    After the second window closes I was expecting the print statement to print. Am I not understanding how exec_() works?

        class Tool1(QtGui.QDialog, ui_tool_1_1.Ui_tool1_1_Dialog):
            """Tool1 dialog"""
    
            # class Initialization
            def __init__(self, parent=None):
    
                # python super so script does not need to call the methods by name.
                super(Tool1, self).__init__(parent)
                self.setupUi(self)
    
                # Push button to open a new gui.
                self.add_evt_pushButton.clicked.connect(self.addEvt)
    
            def addEvt(self):
                test = AppField(parent=self)
                if test.exec_():
                    print"hello"
    
    
        class AppField(QtGui.QDialog, ui_app_field.Ui_AppFieldDialog):
             """
             Tool 2. 
             """
             # class Initialization
            def __init__(self, parent=None):
                super(AppField, self).__init__(parent)
                self.setupUi(self)
    
                # connection to okay button.
                self.ok_pushButton.clicked.connect(self.okay_button)
    
            def okay_button(self):
                """
                handles the ok push button.
                """
                self.close()
    
            def closeEvent(self, event):
                if QtGui.QMessageBox.question(self, "warning", "do you want to close",
                                         QtGui.QMessageBox.Yes|QtGui.QMessageBox.No) == QtGui.QMessageBox.No:
                    event.ignore()
                else:
                    event.accept()


  • Not sure about Phyton, but for c++ i'd use done(int) to exit the dialog.

    So if you have an dialog class myClass and you close it with done(1), myClass.exec() will return 1.

    I'm sure it will be simular for phyton. But i haven't understand the accept and ignore thing, so i cannot explain you why it does not work the way you want to do it



  • My understanding of self.close() got the better of me. self.close() returns self.reject(). That was what I did not understand. self.done(1) would also work, but I think self.accept() is clearer in this case.

    Thank you QT-static-prgm.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.