Qt Designer PyQt5 覆盖 CloseEvent 子窗口不起作用

Posted

技术标签:

【中文标题】Qt Designer PyQt5 覆盖 CloseEvent 子窗口不起作用【英文标题】:Qt Designer PyQt5 overwrite CloseEvent child window don't work 【发布时间】:2017-05-17 16:58:19 【问题描述】:

我在 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_())

【问题讨论】:

【参考方案1】:

closeEvent 方法具有继承自 QWidget 的类,在这种情况下,子类 Sub1Ui_subVen1Ui_subVen2 继承自对象,而不是继承自 QWidget

如果要在 windows 中实现closeEvent,请创建一个使用窗口视图 (Ui_xxx) 并继承自 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_())

【讨论】:

以上是关于Qt Designer PyQt5 覆盖 CloseEvent 子窗口不起作用的主要内容,如果未能解决你的问题,请参考以下文章

PyCharm+PyQt5+Qt Designer配置

PyQt5配合Qt-Designer实现GUI

Pycharm安装PyQt5和pyqt5-tools从而使用Qt Designer

pyqt5.8.2没有qt Designer和assistant exe

PyQt5——Qt Designer初探

pyqt5 Qt Designer + matpoltlib画图显示动态曲线