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 -
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.
-
It is structured differently not mixing the event dispatcher with a widget.
Did you test it ?
What about yours when using SetSuspendState ?