Search code examples
pythonpyqt5spyderqt-designer

closeEvent not running


I have these codes created from the interface I designed in 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 

       

and i have this class that i defined to do a few operations on the imported image.

 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 

I tried to use the closeEvent method to give a warning screen before exiting the application, but it exits the application without calling the method. As I understand, my operations class is just Python object subclass hence it has no closeEvent. I don't want to make big changes in this class. Is there any way I can use the closeEvent method? Or that I somehow open this warning window?


Solution

  • I was calling closeEvent in the wrong place. It's completely pointless to call it inside the class that contains my Python functions. Because it has no inherited relationship with pyqt, but my driver code has this relationship and calling it here solved the problem.

    Driver Code:

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