I have a Qt application that is built on Windows (Laptop). I am using a Windows 11 pro system, I need to get a notification/message when the system goes into sleep/hibernation.
I have used the below code in my application.
#include <QAbstractNativeEventFilter>
#include <QAbstractEventDispatcher>
#include <QDebug>
#include <windows.h>
class MyEventFilter : public QAbstractNativeEventFilter {
public:
virtual bool nativeEventFilter(const QByteArray& eventType, void* message, long*) Q_DECL_OVERRIDE {
MSG* msg = static_cast< MSG* >( message );
if (msg->message == WM_POWERBROADCAST) {
switch (msg->wParam) {
case PBT_APMPOWERSTATUSCHANGE:
qDebug() << ("PBT_APMPOWERSTATUSCHANGE received\n");
break;
case PBT_APMRESUMEAUTOMATIC:
qDebug() << ("PBT_APMRESUMEAUTOMATIC received\n");
break;
case PBT_APMRESUMESUSPEND:
qDebug() << ("PBT_APMRESUMESUSPEND received\n");
break;
case PBT_APMSUSPEND:
qDebug() << ("PBT_APMSUSPEND received\n");
break;
}
}
return false;
}
};
But, I am not receiving the PBT_APMSUSPEND event message when my system goes to sleep/hibernation. (i.e either when I close the laptop manually or click on the sleep option in the power menu) Am I missing something? RegisterSuspendResumeNotification function, can it be used? If so how?
Thanks
Update
The above code works well on the Windows Desktop system but not on the Laptop system.
Update 2
There is no issue with the system and the code will also work smoothly. Only the sleep mode scenario is slightly weird. When the system is put on sleep mode overnight, in the morning when turned on the system actually starts from the bootup, so in this scenario, the PBT_APMSUSPEND
is received.