原文:https://blog.csdn.net/u010139869/article/details/79449315
随笔稍作修改,把继承方法QDialog改为了QMainWindow
关键部分:重写QtWidgets.QMainWindow的closeEvent方法
# 重写closeEvent方法,实现dialog窗体关闭时执行一些代码 from PyQt5 import QtCore, QtGui, QtWidgets import sys class Dialog(QtWidgets.QMainWindow): def get_thread(self, main_thread): self.main_thread = main_thread def closeEvent(self, event): """ 重写closeEvent方法,实现dialog窗体关闭时执行一些代码 :param event: close()触发的事件 :return: None """ reply = QtWidgets.QMessageBox.question(self, '本程序', "是否要退出程序?", QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No, QtWidgets.QMessageBox.No) if reply == QtWidgets.QMessageBox.Yes: event.accept() sys.exit(0) else: event.ignore()
完整代码
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'dialog.ui' # # Created by: PyQt5 UI code generator 5.10.1 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets import sys class Ui_Dialog(object): def setupUi(self, Dialog): Dialog.setObjectName("Dialog") Dialog.resize(640, 480) self.pushButton = QtWidgets.QPushButton(Dialog) self.pushButton.setGeometry(QtCore.QRect(370, 80, 113, 32)) self.pushButton.setObjectName("pushButton") self.lineEdit = QtWidgets.QLineEdit(Dialog) self.lineEdit.setGeometry(QtCore.QRect(140, 80, 113, 21)) self.lineEdit.setObjectName("lineEdit") self.textBrowser = QtWidgets.QTextBrowser(Dialog) self.textBrowser.setGeometry(QtCore.QRect(180, 200, 256, 192)) self.textBrowser.setObjectName("textBrowser") self.label = QtWidgets.QLabel(Dialog) self.label.setGeometry(QtCore.QRect(30, 80, 111, 16)) self.label.setObjectName("label") self.label_2 = QtWidgets.QLabel(Dialog) self.label_2.setGeometry(QtCore.QRect(30, 220, 141, 16)) self.label_2.setObjectName("label_2") self.retranslateUi(Dialog) QtCore.QMetaObject.connectSlotsByName(Dialog) def retranslateUi(self, Dialog): _translate = QtCore.QCoreApplication.translate Dialog.setWindowTitle(_translate("Dialog", "Dialog")) self.pushButton.setText(_translate("Dialog", "PushButton")) self.label.setText(_translate("Dialog", "这是Line Edit ->")) self.label_2.setText(_translate("Dialog", "这是Text Browser ->")) class Dialog(QtWidgets.QMainWindow): """对QDialog类重写,实现一些功能""" def closeEvent(self, event): """ 重写closeEvent方法,实现dialog窗体关闭时执行一些代码 :param event: close()触发的事件 :return: None """ reply = QtWidgets.QMessageBox.question(self, '本程序', "是否要退出程序?", QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No, QtWidgets.QMessageBox.No) if reply == QtWidgets.QMessageBox.Yes: event.accept() else: event.ignore() def main(): """ 主函数,用于运行程序 :return: None """ app = QtWidgets.QApplication(sys.argv) dialog = Dialog() # 注意修改为了自己重写的Dialog类 ui = Ui_Dialog() ui.setupUi(dialog) dialog.show() # 显示了自己重写的Dialog类 sys.exit(app.exec_()) if __name__ == '__main__': main()