问题
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 overwriting the closeEvent in the main window. I need to implement some stuff in the closeEvent of the child window so I overwritten the closeEvent of the child class but it does nothing. Please help.
Class of the main window made in Qt Designer
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_ventanaPrincipal(object):
def setupUi(self, ventanaPrincipal):
ventanaPrincipal.setObjectName("ventanaPrincipal")
ventanaPrincipal.resize(267, 238)
self.centralWidget = QtWidgets.QWidget(ventanaPrincipal)
self.centralWidget.setObjectName("centralWidget")
self.buttonVentana1 = QtWidgets.QPushButton(self.centralWidget)
self.buttonVentana1.setGeometry(QtCore.QRect(60, 30, 141, 25))
self.buttonVentana1.setObjectName("buttonVentana1")
self.buttonVentana2 = QtWidgets.QPushButton(self.centralWidget)
self.buttonVentana2.setGeometry(QtCore.QRect(60, 80, 141, 25))
self.buttonVentana2.setObjectName("buttonVentana2")
ventanaPrincipal.setCentralWidget(self.centralWidget)
self.menuBar = QtWidgets.QMenuBar(ventanaPrincipal)
self.menuBar.setGeometry(QtCore.QRect(0, 0, 267, 22))
self.menuBar.setObjectName("menuBar")
ventanaPrincipal.setMenuBar(self.menuBar)
self.mainToolBar = QtWidgets.QToolBar(ventanaPrincipal)
self.mainToolBar.setObjectName("mainToolBar")
ventanaPrincipal.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
self.statusBar = QtWidgets.QStatusBar(ventanaPrincipal)
self.statusBar.setObjectName("statusBar")
ventanaPrincipal.setStatusBar(self.statusBar)
self.retranslateUi(ventanaPrincipal)
QtCore.QMetaObject.connectSlotsByName(ventanaPrincipal)
def retranslateUi(self, ventanaPrincipal):
_translate = QtCore.QCoreApplication.translate
ventanaPrincipal.setWindowTitle(_translate("ventanaPrincipal", "ventanaPrincipal"))
self.buttonVentana1.setText(_translate("ventanaPrincipal", "Ventana 1"))
self.buttonVentana2.setText(_translate("ventanaPrincipal", "Ventana 2"))
Class of first child window
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_subVen1(object):
def setupUi(self, subVen1):
subVen1.setObjectName("subVen1")
subVen1.resize(320, 347)
self.label = QtWidgets.QLabel(subVen1)
self.label.setGeometry(QtCore.QRect(40, 20, 141, 17))
self.label.setObjectName("label")
self.listWidget = QtWidgets.QListWidget(subVen1)
self.listWidget.setGeometry(QtCore.QRect(20, 60, 256, 192))
self.listWidget.setObjectName("listWidget")
self.retranslateUi(subVen1)
QtCore.QMetaObject.connectSlotsByName(subVen1)
def retranslateUi(self, subVen1):
_translate = QtCore.QCoreApplication.translate
subVen1.setWindowTitle(_translate("subVen1", "Form"))
self.label.setText(_translate("subVen1", "MIAU MIAU"))
Class of second child window
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_subVen2(object):
def setupUi(self, subVen2):
subVen2.setObjectName("subVen2")
subVen2.resize(320, 304)
self.listView = QtWidgets.QListView(subVen2)
self.listView.setGeometry(QtCore.QRect(40, 60, 256, 192))
self.listView.setObjectName("listView")
self.label = QtWidgets.QLabel(subVen2)
self.label.setGeometry(QtCore.QRect(80, 20, 121, 17))
self.label.setObjectName("label")
self.retranslateUi(subVen2)
QtCore.QMetaObject.connectSlotsByName(subVen2)
def retranslateUi(self, subVen2):
_translate = QtCore.QCoreApplication.translate
subVen2.setWindowTitle(_translate("subVen2", "Form"))
self.label.setText(_translate("subVen2", "Guau Guau"))
File that calls the other three.
import sys
#import classes-----------------------
from HMIs.ventanaprincipal import Ui_ventanaPrincipal, QtWidgets
from HMIs.subventana1 import Ui_subVen1
from HMIs.subventana2 import Ui_subVen2
# hijaSub1 inherits the first child window class made by the QtDesigner
#override child closeEvent
class hijaSub1(Ui_subVen1):
def closeEvent(self, event):
print("X is clicked")
class multiVen(QtWidgets.QMainWindow):
def __init__(self,parent=None):
QtWidgets.QWidget.__init__(self,parent=None)
self.ui =Ui_ventanaPrincipal()
self.ui.setupUi(self)
self.subV1=QtWidgets.QWidget()
self.subV2=QtWidgets.QWidget()
#Conect signals whith slots--------------------
self.ui.buttonVentana1.clicked.connect(self.muestraVentana1)
self.ui.buttonVentana2.clicked.connect(self.muestraVentana2)
# slots-----------------------------------------
def muestraVentana1(self):
self.wid1=hijaSub1()
self.wid1.setupUi(self.subV1)
self.subV1.show()
def muestraVentana2(self):
self.wid2=Ui_subVen2()
self.wid2.setupUi(self.subV2)
self.subV2.show()
#Close all windows whith X button of main window
#override main closeEvent
def closeEvent(self, event):
self.subV1.close()
self.subV2.close()
event.accept()
if __name__=="__main__":
app=0
app=QtWidgets.QApplication(sys.argv)
myapp=multiVen()
myapp.show()
sys.exit(app.exec_())
回答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
.
class hijaSub1(QtWidgets.QWidget, Ui_subVen1):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent=parent)
self.setupUi(self)
def closeEvent(self, event):
print("X is clicked: hijaSub1")
class hijaSub2(QtWidgets.QWidget, Ui_subVen2):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent=parent)
self.setupUi(self)
def closeEvent(self, event):
print("X is clicked: hijaSub2")
class multiVen(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-----------------------------------------
def muestraVentana1(self):
self.subV1.show()
def muestraVentana2(self):
self.subV2.show()
#Close all windows whith X button of main window
#override main closeEvent
def closeEvent(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_())
来源:https://stackoverflow.com/questions/44030849/qt-designer-pyqt5-overwrite-closeevent-child-window-dont-work