Skip to content Skip to sidebar Skip to footer

Qt Designer Pyqt5 Overwrite Closeevent Child Window Don't Work

I have three windows designed in QtDesigner. The main window calls the two childs windows using buttons. If I close the main window the childs windows also close, I did this by ove

Solution 1:

The closeEvent method has the classes that inherit from QWidget, in which case the daughter classes Sub1, Ui_subVen1, Ui_subVen2 inherit from object, not from QWidget.

If you want to implement closeEvent in windows, create a class that uses the window view (Ui_xxx) and inherits from QWidget.

classhijaSub1(QtWidgets.QWidget, Ui_subVen1):
    def__init__(self, parent=None):
        QtWidgets.QWidget.__init__(self, parent=parent)
        self.setupUi(self)

    defcloseEvent(self, event):
        print("X is clicked: hijaSub1")

classhijaSub2(QtWidgets.QWidget, Ui_subVen2):
    def__init__(self, parent=None):
        QtWidgets.QWidget.__init__(self, parent=parent)
        self.setupUi(self)

    defcloseEvent(self, event):
        print("X is clicked: hijaSub2")

classmultiVen(QtWidgets.QMainWindow):
    def__init__(self,parent=None):
        QtWidgets.QWidget.__init__(self,parent=None)
        self.ui =Ui_ventanaPrincipal()
        self.ui.setupUi(self)
        self.subV1=hijaSub1()
        self.subV2=hijaSub2()
        #Conect signals whith slots--------------------
        self.ui.buttonVentana1.clicked.connect(self.muestraVentana1)
        self.ui.buttonVentana2.clicked.connect(self.muestraVentana2)
    # slots-----------------------------------------defmuestraVentana1(self):
        self.subV1.show() 

    defmuestraVentana2(self):
        self.subV2.show()
#Close all windows whith X button of main window#override main closeEvent        defcloseEvent(self, event):
        self.subV1.close()
        self.subV2.close()
        event.accept()


if __name__=="__main__":
    import sys
    app=0
    app=QtWidgets.QApplication(sys.argv)
    myapp=multiVen()
    myapp.show()
    sys.exit(app.exec_())

Post a Comment for "Qt Designer Pyqt5 Overwrite Closeevent Child Window Don't Work"