如何在 Qt 中检测 Windows 关闭或注销 [英] How to detect Windows shutdown or logoff in Qt

查看:101
本文介绍了如何在 Qt 中检测 Windows 关闭或注销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一个 Linux 应用程序移植到用 Qt 编写的 Windows.应用程序需要在关闭前保存一些设置.在 Linux 上,我们可以通过 SIGTERM 等的信号处理程序来实现.我如何在 Windows 上实现相同的功能.

I am porting a Linux app to Windows written in Qt. The application needs to save some settings before closing. On Linux, we can do that by signal handlers for SIGTERM etc. How can I implement the same on Windows.

推荐答案

如果您正在使用 Qt 事件循环,您可以捕获以下信号:

If you are using the Qt event loop, you can catch the following signal:

void QCoreApplication::aboutToQuit() [信号]

void QCoreApplication::aboutToQuit() [signal]

当应用程序即将退出主事件循环时发出此信号,例如当事件循环级别降至零时.这可能发生在从应用程序内部调用 quit() 之后或当用户关闭整个桌面会话时.如果您的应用程序必须进行一些最后一秒的清理,则该信号特别有用.请注意,在此状态下无法进行用户交互.

This signal is emitted when the application is about to quit the main event loop, e.g. when the event loop level drops to zero. This may happen either after a call to quit() from inside the application or when the users shuts down the entire desktop session. The signal is particularly useful if your application has to do some last-second cleanup. Note that no user interaction is possible in this state.

除此之外,如果上述信号不适合您的用例,您可能正在寻找以下消息:

Other than that, you may be looking for the following messages below if the aforementioned signal is not appropriate for your use case:

WM_QUIT:http:///msdn.microsoft.com/en-us/library/windows/desktop/ms632641(v=vs.85).aspx

WM_CLOSE:http:///msdn.microsoft.com/en-us/library/windows/desktop/ms632617(v=vs.85).aspx

WM_QUERYENDSESSION:http://msdn.microsoft.com/en-US/library/windows/desktop/aa376890.aspx

WM_QUERYENDSESSION: http://msdn.microsoft.com/en-US/library/windows/desktop/aa376890.aspx

WM_ENDSESSION:http://msdn.microsoft.com/en-US/library/windows/desktop/aa376889.aspx

WM_ENDSESSION: http://msdn.microsoft.com/en-US/library/windows/desktop/aa376889.aspx

这篇关于如何在 Qt 中检测 Windows 关闭或注销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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