Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Receive event when pc wake up



  • Hi all,

    I am new with Qt. I know that this topic is old and probably it's solved but I don't find the solution. I am trying to implement a code that runs when the pc returns from suspend state. I've tried with WM_POWERBROADCAST but doesn't work.
    Attached my code:

    header:

      #include <QAbstractNativeEventFilter>
      #include <QObject>
      #include <QWidget>
      #include <windows.h>
    
     class windowsEvents : public QWidget, public QAbstractNativeEventFilter
     {   
          public:
                 windowsEvents();
                 virtual bool nativeEventFilter(const QByteArray &eventType, void *message, long *) Q_DECL_OVERRIDE;
     };
    

    class:

    #include <winuser.h>
    
    windowsEvents::windowsEvents()
    {
        RegisterSuspendResumeNotification((HANDLE)this, DEVICE_NOTIFY_WINDOW_HANDLE);
    }
    
    bool windowsEvents::nativeEventFilter(const QByteArray &eventType, void *message, long *) {
    
        MSG *msg = static_cast<MSG *>(message);
    
        if (msg->message == WM_POWERBROADCAST) {
            if (msg->wParam == PBT_APMRESUMESUSPEND ) {
                //code
            }
        }
    return false;
    }
    

    Main:

    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
    
        windowsEvents devEventFilterObj;
        QAbstractEventDispatcher::instance()->installNativeEventFilter(&devEventFilterObj);
    
       return app.exec();
    }
    

    What is it wrong? What do i need to change?
    Thanks you


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You might want to check the implementation from this thread.



  • @SGaist

    Thanks for your reply. I am checking that thread and I can't find any difference with my code.

    In the thread it used rundll32.exe powrprof.dll, SetSuspendState 0,1,0 to sleep but I don't use those commands. I use the Windows key -> suspend. Could is it the problem?

    Thank you in advance.


  • Lifetime Qt Champion

    It is structured differently not mixing the event dispatcher with a widget.

    Did you test it ?
    What about yours when using SetSuspendState ?



  • @SGaist thanks for your answers.
    Finally, I found the solution. I didn't use the right handle for register solution.


Log in to reply