Qt - 检测windows系统休眠

文章介绍了在Windows+Qt环境下,如何利用`nativeEvent`函数处理一体机上的关机和休眠事件,以保存配置和用户操作信息。在`nativeEvent`的重写中,特别关注了`WM_POWERBROADCAST`消息,用于识别系统挂起事件,以便执行相应的缓存保存操作。
摘要由CSDN通过智能技术生成

Windows+Qt方案

场景

1.产品搭建在一体机上,需要关机缓存配置和用户操作信息

2.面对用户的关机/休眠设置,软件需要保留用户设置

方案

[virtual protected] bool QWidget::nativeEvent(const QByteArray &eventType, void *message, long *result)

This special event handler can be reimplemented in a subclass to receive native platform events identified by eventType which are passed in the message parameter.
In your reimplementation of this function, if you want to stop the event being handled by Qt, return true and set result. The result parameter has meaning only on Windows. If you return false, this native event is passed back to Qt, which translates the event into a Qt event and sends it to the widget.
Events are only delivered to this event handler if the widget has a native window handle.
Note: This function superseedes the event filter functions x11Event(), winEvent() and macEvent() of Qt 4.

这个特殊的事件处理程序可以在子类中重新实现,以接收由 eventType 标识的本机平台事件,这些事件在消息参数中传递。
在您重新实现此函数时,如果您想停止 Qt 正在处理的事件,请返回 true 并设置结果。 结果参数仅在 Windows 上有意义。 如果返回 false,则此本机事件将传回 Qt,Qt 将事件转换为 Qt 事件并将其发送到小部件。
如果小部件具有本机窗口句柄,则事件仅传递给此事件处理程序。
注意:此函数取代了 Qt 4 的事件过滤器函数 x11Event()、winEvent() 和 macEvent()。

PlatformEvent Type IdentifierMessage TypeResult Type
Windows"windows_generic_MSG”MSG *LRESULT
macOS"NSEvent”NSEvent *
XCB"xcb_generic_event_t”xcb_generic_event_t *
//for .h 
#ifdef Q_OS_WIN32
#include "Windows.h"
#endif

class MainWindow : public QMainWindow
{
    Q_OBJECT
 
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
protected:
    bool nativeEvent(const QByteArray &eventType, void *message, long *result) Q_DECL_OVERRIDE;
 
private:
    Ui::MainWindow *ui;
};

//for .cpp
bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
    Q_UNUSED(result);
 
    if (eventType == "windows_generic_MSG") {
#ifdef Q_OS_WIN32
        MSG* msg = static_cast<MSG*>(message);
        if (msg->message == WM_POWERBROADCAST
                && msg->wParam == PBT_APMSUSPEND) {
           //TODO:add your codes
        }
#endif
    } else if (eventType == "NSEvent") {
#ifdef Q_OS_MACOS
        //TODO: add your codes
#endif
    }
 
    return false;
}

qt-opensource-windows5.4是指Qt开源框架的Windows版本5.4。 Qt是一款跨平台的应用程序开发框架,可以通过统一的代码编写方式,在不同的操作系统上运行程序。Qt提供了丰富的功能和工具,使开发者可以快速构建图形用户界面(GUI)应用程序。 Qt开源框架的Windows版本5.4是指适用于Windows操作系统Qt开源框架的第五个主要版本,发布于2014年。这个版本为开发者提供了许多新功能和改进,以提高程序的性能和可靠性。 Qt-opensource-windows5.4版本具有以下特点: 1. 支持Windows平台:Qt-opensource-windows5.4版本是专门针对Windows操作系统开发的,可以在不同的Windows版本上运行。 2. 开源框架:Qt采用开源模式,使开发者可以自由地使用和修改框架,以满足不同的应用需求。 3. 丰富的GUI开发工具:Qt提供了各种用于开发图形用户界面的工具和组件,可以轻松创建出美观、交互性强的应用程序。 4. 跨平台:Qt的设计理念是“一次编写,到处运行”,使用Qt开发的应用程序可以在不同的操作系统上进行移植,包括Windows、macOS、Linux等。 5. 多种编程语言支持:Qt支持多种编程语言,包括C++、Python、Java等,开发者可以根据自己的习惯选择合适的语言进行开发。 总之,Qt-opensource-windows5.4是一个功能强大的开源框架,适用于Windows平台的应用程序开发。它提供了丰富的功能和工具,使开发者可以轻松创建出高性能、跨平台的图形用户界面应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值