[SOLVED] ScreenSaver - disable screensaver on Windows



  • Trying to disable screensaver on windows when a specific QDialog is open.

    I tried a lot of way but can't get it to work on Windows, with Mac I found a way.

    Even tried to send key event and mouse event to the QDialog, event works fine but don't stop the screensaver from coming

    Method 1 (hacky):

    void WorkoutDialog::postponeScreensaver() {
    
      QPoint toMove = this->mapFromGlobal(QCursor::pos());
      toMove.setX(toMove.x()+1);
    
      QTest::keyClick(this, Qt::Key_A); //doesnt postpone screensaver...
      QTest::mouseMove(this, toMove); //mouse move but screensaver still trigger
    }
    

    Method 2 (not working on all Windows platform):

    SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, FALSE , NULL, SPIF_SENDWININICHANGE);
    

    Other Method?



  • This Method seems to be working, require more testing:

    #ifdef Q_OS_MAC
      macUtil.disableScreensaver();
    #endif
    #ifdef Q_OS_WIN32
      SetThreadExecutionState(ES_CONTINUOUS | ES_DISPLAY_REQUIRED);
      //SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, FALSE , NULL, SPIF_SENDWININICHANGE);
    #endif


  • May be something worth to have by default in Qt to avoid #ifdef

    OSX: MacUtils.mm

    //http://stackoverflow.com/questions/5596319/how-to-programmatically-prevent-a-mac-from-going-to-sleep/5596946#5596946
    void MacUtils::disableScreensaver() {
    
    	 qDebug() << "disableScreenSaveronMAC!";
    
    	CFStringRef reasonForActivity = CFSTR("Maximum Trainer Workout");
    
    	IOReturn success = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoDisplaySleep,
    										kIOPMAssertionLevelOn, reasonForActivity, &assertionID);
    
    
    	if (success == kIOReturnSuccess)
    	{
    		//success = IOPMAssertionRelease(assertionID);
    		///return disable sucess? todo..
    	}
    
    	qDebug() << "disableScreenSaveronMAC end !";
    }
    void MacUtils::releaseScreensaverLock() {
    
    	IOPMAssertionRelease(assertionID);
    }
    

    Windows
    SetThreadExecutionState(ES_CONTINUOUS | ES_DISPLAY_REQUIRED);


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.