Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Receive event when pc wake up
Forum Update on Monday, May 27th 2025

Receive event when pc wake up

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 1.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bpar
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by SGaist
      #2

      Hi and welcome to devnet,

      You might want to check the implementation from this thread.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      B 1 Reply Last reply
      2
      • SGaistS SGaist

        Hi and welcome to devnet,

        You might want to check the implementation from this thread.

        B Offline
        B Offline
        bpar
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          B 1 Reply Last reply
          1
          • SGaistS SGaist

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

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

            B Offline
            B Offline
            bpar
            wrote on last edited by
            #5

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

            1 Reply Last reply
            2

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved