1

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.

  • Each event has its specific cause. When do you receive this message? What happens with your computer? – vahancho May 09 '23 at 10:05
  • When my system goes to sleep/hibernation, I must receive the PBT_APMSUSPEND event. But It's not happening. – Qt Beginner May 09 '23 at 10:08
  • The docs say that "...This event is typically broadcast when all applications and installable drivers have returned TRUE to a previous PBT_APMQUERYSUSPEND event." – vahancho May 09 '23 at 10:10
  • Sorry to ask, but what does that indicate? I believe the end of support for the PBT_APMQUERYSUSPEND event is Windows XP. – Qt Beginner May 09 '23 at 10:41
  • I can receive PBT_APMSUSPEND event on my WIN10 system. Have you called `installNativeEventFilter`? – Strive Sun May 09 '23 at 11:42
  • BTW, [the case](https://forum.qt.io/topic/69485/do-an-operation-when-computer-goes-to-sleep/3) works for me. – Strive Sun May 09 '23 at 11:43
  • @StriveSun I am using Windows 11 system, In [this case](https://forum.qt.io/topic/69485/do-an-operation-when-computer-goes-to-sleep) it is mentioned that tested in Windows 10 only. I am searching for an OS version-independent solution. PBT_APMPOWERSTATUSCHANGE is working fine for me but the other 3 are not. – Qt Beginner May 09 '23 at 12:02
  • Please don't add "solved" to your question title or body. See [what should I do when someone answers](https://stackoverflow.com/help/someone-answers) on how to show you've solved your problem. Neither add answers to the question body itself. Instead, you should add it as an answer. [Answering your own question is allowed and even encouraged](https://stackoverflow.com/help/self-answer). – Adriaan May 16 '23 at 07:40

1 Answers1

0

I tested in my another computer(Windows 11). When the system goes to sleep, I received PBT_APMSUSPEND, PBT_APMRESUMESUSPEND, PBT_APMRESUMEAUTOMATIC event. But PBT_APMPOWERSTATUSCHANGE not be received.

I suspect you haven't put the computer into sleep in a reasonable way.

Try to click Sleep icon as the screenshot showed below.

enter image description here

This is a minimal example, works on WIN10 and WIN11

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;
  }
};

int main() {
  int argc = 1;
  char* argv = new char[MAX_PATH];
  QApplication::setAttribute(Qt::AA_DisableHighDpiScaling);
  QApplication app(argc, &argv);

  QMainWindow main_window;
  main_window.setFixedSize(200, 100);
  main_window.show();

  QAbstractEventDispatcher::instance()->installNativeEventFilter(
      new MyEventFilter);

  app.exec();
  return 0;
}

Update:

I notice you said only the PBT_APMPOWERSTATUSCHANGE is received when I plug in/unplug to power and the other three are not working.

This is by design.

From PBT_APMPOWERSTATUSCHANGE,

Notifies applications of a change in the power status of the computer, such as a switch from battery power to A/C. The system also broadcasts this event when remaining battery power slips below the threshold specified by the user or if the battery power changes by a specified percentage.

The computer go to sleep does not change the battery status.

Strive Sun
  • 5,988
  • 1
  • 9
  • 26
  • Thanks for the reply. I have tried all the possible ways to get my system into sleep mode but none worked for me. BTW `PBT_APMPOWERSTATUSCHANGE` works when we plug in or unplug the power cable. – Qt Beginner May 10 '23 at 06:28
  • @QtBeginner See my updated. The computer go to sleep does not change the battery status. – Strive Sun May 10 '23 at 06:33
  • @QtBeginner **Sleep** only make the computer enter a suspended state. – Strive Sun May 10 '23 at 06:37
  • I got your point. But still, my issue is not resolved. – Qt Beginner May 10 '23 at 07:19
  • @QtBeginner The issue you described conflicts with the problem you actually encountered. You cannot expect to receive `PBT_APMSUSPEND` event when plug in or unplug the power cable. On the contrary, if you only need to monitor whether the power cable is plugged in or unplugged, `PBT_APMPOWERSTATUSCHANGE` is suitable for you. – Strive Sun May 10 '23 at 07:25
  • No no @Strive, my problem is when my system goes to sleep (either automatically or manually) I didn't receive the `PBT_APMSUSPEND` event. – Qt Beginner May 10 '23 at 07:31
  • @QtBeginner Fine. How do you do to make your system goes to sleep (either automatically or manually)? Such as, click Sleep icon? – Strive Sun May 10 '23 at 07:34
  • The first method is the one you said above, the next is closing the laptop manually which will automatically go to sleep. Or by setting auto sleep time in the setting. – Qt Beginner May 10 '23 at 07:41
  • @QtBeginner OK, maybe you need to delete plug in/unplug to power.. characters in your question and add closing the laptop manually... to make question clear. – Strive Sun May 10 '23 at 07:47
  • @QtBeginner Have you tried my code sample? – Strive Sun May 10 '23 at 07:48
  • Yes, I have tried your code sample too. – Qt Beginner May 10 '23 at 08:02
  • @QtBeginner Strange. It seems only not work on your computer. Can you test on another computer? – Strive Sun May 10 '23 at 08:09
  • 1
    Sure! Will update soon. – Qt Beginner May 10 '23 at 08:11
  • It worked on the Desktop system, but not working on Laptops. My use case is on a Laptop :( – Qt Beginner May 10 '23 at 12:56
  • @QtBeginner No, my Win11 is a Laptop, and it worked. – Strive Sun May 11 '23 at 03:05
  • Could this issue be caused because of Qt configuration? If so, Can you share your Qt version, Compiler configuration details, ( i.e MSVC - version, kits installed, etc) – Qt Beginner May 11 '23 at 06:30
  • @QtBeginner msvc2019, Qt5.15.9 and defalut Compiler configure(add Qt header and Qt libs), btw, I compile on visual studio 2019 which works with Qt VS Tools. Hope to help u – Strive Sun May 11 '23 at 06:49
  • Thanks. My objective is to write into a log file when my system goes to sleep & resumes from sleep. Still, I am unable to get the `PBT_APMSUSPEND` event when I close my laptop to sleep. – Qt Beginner May 12 '23 at 07:12