我在QtDesigner中设计了三个窗口。主窗口使用按钮调用两个子窗口。如果我关闭主窗口,子窗口也关闭,我通过覆盖主窗口中的closeEvent来做到这一点。我需要在子窗口的closeEvent中实现一些东西,所以我覆盖了子类的closeEvent但它什么也没做。请帮忙。
在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"))
第一个儿童窗口类
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"))
第二个儿童窗口类
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"))
调用其他三个的文件。
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_())
答案 0 :(得分:0)
<Button>
方法具有从QWidget继承的类,在这种情况下,子类closeEvent
,Sub1
,Ui_subVen1
继承自对象,而不是{{1 }}
如果要在Windows中实现Ui_subVen2
,请创建一个使用窗口视图(QWidget
)并继承自closeEvent
的类。
Ui_xxx