Search code examples
pythonuser-interfacepyqtpyqt4signals-slots

Is there a way to call a function right before a PyQt application ends?


I am collecting usage stats for my applications which include how much each session lasts. However, I can't seem to be able to save this information because None Of the signals I tried yet actually succeeds to call my report_session function.

This are the signals I have already tried:

  1. lastWindowClosed()
  2. aboutToQuit()
  3. destroyed()

Either these signals never get emitted or the application does not live long enough after that to run anything else. Here is my main:

app = QtGui.QApplication(sys.argv)

ui = MainWindow()
ui.app = app
QtCore.QObject.connect(ui, QtCore.SIGNAL("destroyed()"),  ui.report_session)
ui.show()
logger.info('Started!')
splash.finish(ui)

sys.exit(app.exec_())

Solution

  • Found this answer which involves overloading closeEvent().

    it worked perfectly for me.