11

How can i detect when a users computer goes into sleep (laptop lid closes, sleep mode due to inactivity, etc)?

I need to do this to disconnect the users TCP connection. Basically we got a simple chat application where we want to take the user off-line.

Nejat
  • 30,452
  • 12
  • 97
  • 127
user3490755
  • 915
  • 2
  • 15
  • 31

3 Answers3

9

There is no Qt way to detect when computer goes to sleep or hibernation. But there are some platform dependent ways to do it.

On Windows you can listen for the WM_POWERBROADCAST message in your WindowProc handler:

LRESULT WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
  if (WM_POWERBROADCAST == message && PBT_APMSUSPEND == wParam) {
    // Going to sleep
  }
}

On linux you can put the following shell script in /etc/pm/sleep.d which executes a program with arguments. You can start a program and notify your main application in some way:

#!/bin/bash
case $1 in
suspend)
    #suspending to RAM
    /Path/to/Program/executable Sleeping
    ;;
resume)
    #resume from suspend
    sleep 3
    /Path/to/Program/executable Woken
    ;;
esac

For OS X you can see this.

Nejat
  • 30,452
  • 12
  • 97
  • 127
0

You need to use the QNetworkConfigurationManager class available in Qt 4.7 and above.

QNetworkConfigurationManager provides access to the network configurations known to the system and enables applications to detect the system capabilities (with regards to network sessions) at runtime.

In particular look at the void QNetworkConfigurationManager::onlineStateChanged(bool isOnline) signal.

Silas Parker
  • 7,829
  • 27
  • 43
  • I tried this on my Mac, and putting my Mac to sleep did not cause the onlineStateChanged(bool) signal to be emitted. Nothing in the QNetworkConfigurationManager documentation suggests that it ought to be emitted, either, since that class handles network configuration, not sleep/wake events. – Jeremy Friesner Nov 06 '14 at 01:41
0

You may use 2 QTimers. One timer to activate slot every period of time and the second is to keep time tracking. Something like this:

// Header
QTimer timerPeriod;
QTimer timerTracker;

// Source
timerPeriod.setInterval(60*1000);
connect(&timerPeriod, SIGNAL(timeout()), this, SLOT(timerTimeout()));

// Track time to the next midnight
timerTracking.setInterval(QDateTime::currentDateTime().msecsTo(QDateTime(QDate::currentDate().addDays(1), QTime(00, 00)))); 
timerPeriod.start();
timerTracking.start();

// SLOT    
void timerTimeout() {
   int difference = abs(timerTracking.remainingTime() - QDateTime::currentDateTime().msecsTo(QDateTime(QDate::currentDate().addDays(1), QTime(00, 00))));

   // There are some diffrences in times but it is rather irrelevant. If 
   if (difference > 500) { 
      diffrence > 500 timerTracking should be reset

      // If diffrence is > 2000 it is sure hibernation or sleep happend
      if (difference > 2000) { 
         // Hibernation or sleep action
      }

      // Taking care of small and big diffrences by reseting timerTracking
      timerTracking.stop(); 
      timerTracking.setInterval(QDateTime::currentDateTime().msecsTo(QDateTime(QDate::currentDate().addDays(1), QTime(00, 00))));
      timerTracking.start();
   }
}
MKAROL
  • 306
  • 3
  • 11