Qt detect when computer goes into sleep?

后端 未结 3 406
半阙折子戏
半阙折子戏 2021-01-17 20:16

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 connecti

相关标签:
3条回答
  • 2021-01-17 20:28

    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.

    0 讨论(0)
  • 2021-01-17 20:32

    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.

    0 讨论(0)
  • 2021-01-17 20:44

    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();
       }
    }
    
    0 讨论(0)
提交回复
热议问题