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()。
Platform | Event Type Identifier | Message Type | Result 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;
}