How do I set an event to run when the app ends in Pyqt5?

Asked

Viewed 31 times

0

I am creating an application in Pyqt5 and need to run a code when the application is finishing. For that, I thought about overwriting the method quit class QApplication:

class MyApp(QApplication):
    def quit(self):
        print("Meu código...")
        super().quit()

The problem is that this method is not running when the program ends. My question is: how can I define an event "on_quit" to the QApplication in Pyqt5?

1 answer

0

there is the event closeEvent which may help in this case, it is called when the user closes the window, see the example:

from PyQt5.QtWidgets import *
import sys


class Test(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setGeometry(100,100,600,600)


    def closeEvent(self,e):
        # sua logica antes do programa fechar 
        print("print executando antes de fechar a aplicacao")
        #fim do codigo a ser executado antes do programa fecha
        e.accept()
        



app = QApplication(sys.argv)
w = Test()
w.show()
sys.exit(app.exec_())
                   
  • Yes, I know this event but it’s not what I’m looking for. I really need to create an "on_quit" for the application, without relying on widgets.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.