有没有办法在 PyQt 应用程序结束之前调用函数? [英] Is there a way to call a function right before a PyQt application ends?

查看:39
本文介绍了有没有办法在 PyQt 应用程序结束之前调用函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的应用程序收集使用情况统计信息,其中包括每个会话的持续时间.但是,我似乎无法保存此信息,因为我尝试过的信号中没有一个实际上成功调用了我的 report_session 函数.

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. 销毁()

要么这些信号永远不会发出,要么应用程序在此之后的寿命不够长,无法运行其他任何东西.这是我的主要内容:

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

推荐答案

Found 这个答案 涉及重载 closeEvent().

Found this answer which involves overloading closeEvent().

它非常适合我.

这篇关于有没有办法在 PyQt 应用程序结束之前调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆