有没有办法在 PyQt 应用程序结束之前调用函数?

发布于 2024-09-25 22:26:22 字数 505 浏览 4 评论 0 原文

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

这是我已经尝试过的信号:

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

揽清风入怀 2024-10-02 22:26:22

Mark Byers 发布的方法将在主小部件关闭后运行,这意味着其控件将不再可用。

如果您需要使用表单上控件中的任何值,您将需要捕获关闭事件并在那里进行工作:

class MainWidget(QtGui.QWidget):

    #...

    def closeEvent(self, event):
        print "closing PyQtTest"
        self.SaveSettings()
        # report_session()

另外,请参阅 ZetCode 教程中的消息框示例 PyQt4 工具包中的第一个程序(靠近页面末尾)。这显示了如何接受或取消关闭请求。

The method that Mark Byers posted will run after the main widget has been closed, meaning that its controls will no longer be available.

If you need to work with any values from controls on your form, you will want to capture the close event and do your work there:

class MainWidget(QtGui.QWidget):

    #...

    def closeEvent(self, event):
        print "closing PyQtTest"
        self.SaveSettings()
        # report_session()

Also, see the Message Box example in the ZetCode tutorial First programs in PyQt4 toolkit (near the end of the page). This shows how to accept or cancel the close request.

东北女汉子 2024-10-02 22:26:22

为了确保在进程终止时调用 Python 函数,通常(无论是否涉及 Qt;-),您可以使用 atexit 模块:

import atexit

def whatever(): ...

atexit.register(whatever)

出于谨慎考虑,我建议不要使用绑定方法而不是函数来实现此目的——它“应该”工作,但是 a 的破坏阶段过程总是有些微妙,越简单越好。

当然,atexit 不会触发进程的严重崩溃(例如,如果使用 kill -9 终止进程,则通过定义它没有机会运行任何终止代码)——操作系统会注意到这一点;-)。如果您需要处理任何崩溃,无论您多么努力,都必须通过单独的“看门狗”进程来处理,这是一个更加微妙的问题。

To ensure that a Python function gets called at process termination, in general (with or without Qt involved;-), you can use the atexit module of the standard Python library:

import atexit

def whatever(): ...

atexit.register(whatever)

Out of prudence I would recommend against using a bound method instead of a function for this purpose -- it "should" work, but the destruction-phase of a process is always somewhat delicate, and the simpler you keep it, the better.

atexit won't trigger for a sufficiently-hard crash of a process, of course (e.g., if the process is killed with a kill -9, then by definition it's not given a chance to run any termination code) -- the OS sees to that;-). If you need to handle any crash no matter how hard you must do so from a separate "watchdog" process, a substantially subtler issue.

浪荡不羁 2024-10-02 22:26:22

将代码放在 app.exec_sys.exit 之间:

ret = app.exec_()
# Your code that must run when the application closes goes here
sys.exit(ret)

Put the code between app.exec_ and sys.exit:

ret = app.exec_()
# Your code that must run when the application closes goes here
sys.exit(ret)
银河中√捞星星 2024-10-02 22:26:22

找到这个答案,其中涉及重载closeEvent()。

它对我来说非常有效。

Found this answer which involves overloading closeEvent().

it worked perfectly for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文