closeEvent 不是 运行

closeEvent not running

我从我在 qt designer 中设计的界面创建了这些代码。

    # -*- coding: utf-8 -*-
    # Form implementation generated from reading ui file 'qt.ui'
    # Created by: PyQt5 UI code generator 5.15.4
    # WARNING: Any manual changes made to this file will be lost when pyuic5 is
    # run again.  Do not edit this file unless you know what you are doing.
    
    from PyQt5 import QtCore, QtGui, QtWidgets
    from PyQt5.QtWidgets import QFileDialog , QMessageBox , QDesktopWidget , QApplication , QInputDialog , QMainWindow , QAction , QWidget
    from PyQt5.QtGui import QIcon, QPixmap
    from PyQt5.QtCore import pyqtSignal , QObject
    from PyQt5.QtGui     import *
    import sys
    import os
    import operations
    
    class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            MainWindow.setObjectName("MainWindow")
            MainWindow.resize(1584, 657)
bla bla bla 

       

我有这个class,我定义它对导入的图像进行一些操作。

 from skimage import data , io , filters , color , segmentation , img_as_float
 from PyQt5.QtWidgets import QFileDialog , QMessageBox , QDesktopWidget
 from PyQt5.QtGui import QPixmap
 import numpy as np
 import os
 from abc import ABCMeta,abstractmethod

 class Operations():
     def __init__(self):  
         coffee = data.coffee()
         io.imsave('sample_images/coffee.jpg',coffee)
         camera = data.camera()
         io.imsave('sample_images/camera.jpg',camera)
         horse = data.horse()
         io.imsave('sample_images/horse.jpg',horse)
         self.history = []
         self.operation_list = ['first_operation','rgb2gray','rgb2hsv','multiOtsuTh','chanVeseSeg',
                                'morphoSnakes','roberts','sobel','scharr','prewitt']
         self.active = False 
bla bla bla 

我尝试使用closeEvent方法在退出应用程序之前给出警告屏幕,但它没有调用该方法就退出了应用程序。 据我了解,我的操作 class 只是 Python object subclass 因此它没有 closeEvent。 我不想对此 class 进行大的更改。有什么办法可以使用 closeEvent 方法吗?或者我以某种方式打开了这个警告 window?

我在错误的地方调用了 closeEvent。在包含我的 Python 函数的 class 中调用它是完全没有意义的。因为它和pyqt没有继承关系,但是我的driver代码有这个关系,在这里调用解决了问题。

Driver代码:

from PyQt5 import QtWidgets 
from PyQt5.QtWidgets import QApplication, QMessageBox, QMainWindow, QAction
from qt import Ui_MainWindow
import sys

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        
        # Set up the user interface from Designer.
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        
    #closing alert
    def closeEvent(self, event):
        close = QMessageBox()
        close.setWindowTitle("Window Close")
        close.setIcon(QMessageBox.Critical)
        close.setText("Are you sure you want to close the window?")
        close.setStandardButtons(QMessageBox.Yes | QMessageBox.Cancel)
        close.setDefaultButton(QMessageBox.Cancel)
        
        #If there is no output or if the output has already been saved in a way, do not ask if it is desired to save when closing.
        if(self.ui.pB_clearOutput.isEnabled() == False or self.ui.pB_exportAsOutput.isEnabled() == False or self.ui.pB_saveAsOutput.isEnabled() == False or self.ui.pB_saveOutput.isEnabled() == False):
            close = close.exec()
            if close == QMessageBox.Yes:
                event.accept()
            else:
                event.ignore()
        #else ask the user for if it is desired to save when closing.
        else:
            close.setStandardButtons(QMessageBox.Yes | QMessageBox.Save | QMessageBox.Cancel)
            close = close.exec()
            if close == QMessageBox.Yes:
                print("Yes")
                event.accept()
            elif close == QMessageBox.Save:
                print("Save")
                self.op.saveAsOutputImage(self.ui)
            else:
                print("Ignore")
                event.ignore()
            

app = QApplication(sys.argv)
MainWindow = MainWindow()
MainWindow.show()
sys.exit(app.exec_())