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. [SOLVED] QDialog in fullscreen - disable OS screensaver?

[SOLVED] QDialog in fullscreen - disable OS screensaver?

Scheduled Pinned Locked Moved General and Desktop
29 Posts 5 Posters 15.2k 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.
  • M Offline
    M Offline
    maximus
    wrote on 3 Mar 2014, 02:02 last edited by
    #1

    Hi,

    I would like to know if it's possible to disable the OS screensaver from a QMainWindow or QDialog. If not, would it be possible to "hack" it and emulate mouse movement or keyboard press at interval time x so the screensaver doesn't show. It is important not to have a screensaver, since the user is away from the computer when using this application.

    Thank you

    Code sample :

    @ WorkoutDialog w(tableModel->getWorkoutAtRow(sourceIndex), this);
    w.setModal(true);
    w.showFullScreen();

        if (w.exec() == QDialog::Accepted) {
            //        qDebug() << "workout done!";
        }
        else {
            //        qDebug() << "workout cancel!";
        }@
    

    Free Indoor Cycling Software - https://maximumtrainer.com

    1 Reply Last reply
    0
    • H Offline
      H Offline
      hskoglund
      wrote on 3 Mar 2014, 02:42 last edited by
      #2

      If this is for Windows, I've read somewhere that you can use the SendInput() API, you set up a timer say twice a minute and do something similar to this:
      @ INPUT Mouse;

      Mouse.type = INPUT_MOUSE;
      Mouse.mi.dwFlags = MOUSEEVENTF_MOVE;
      ::SendInput(1,&Mouse,sizeof(INPUT));@
      

      This should emulate a mouse movement with zero delta x and zero delta y, should be enough to make the screensaver not start.

      Haven't tried, but I found "this thread on Stackoverflow":http://stackoverflow.com/questions/463813/programmatically-prevent-windows-screensaver-from-starting
      , somewhere in the middle there is code for this, in VB.NET though :-(

      1 Reply Last reply
      0
      • M Offline
        M Offline
        maximus
        wrote on 3 Mar 2014, 02:44 last edited by
        #3

        I'll check if I can find a cross-platform solution (Mac/Win), if not this will do the job, thanks a lot!


        Free Indoor Cycling Software - https://maximumtrainer.com

        1 Reply Last reply
        0
        • M Offline
          M Offline
          maximus
          wrote on 10 Apr 2014, 18:08 last edited by
          #4

          I found this :
          http://developer.nokia.com/community/wiki/Inhibiting_the_device_screen_saver_using_Qt

          But I can't include "QSystemScreenSaver", maybe it's no longer part of Qt? Using 5.2.1


          Free Indoor Cycling Software - https://maximumtrainer.com

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 10 Apr 2014, 21:09 last edited by
            #5

            Hi,

            It was part of QtMobility and AFAICS, it's not been ported (yet ?) to Qt 5

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

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MarianMMX
              wrote on 11 Apr 2014, 10:01 last edited by
              #6

              On Windows:
              "SetThreadExecutionState":http://msdn.microsoft.com/en-us/library/aa373208(VS.85).aspx

              @
              #include <Windows.h>
              void DisableScreenSaver()
              {
              SetThreadExecutionState(EXECUTION_STATE.ES_DISPLAY_REQUIRED | EXECUTION_STATE.ES_CONTINUOUS);
              }
              void EnableScreenSaver()
              {
              SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS);
              }

              @

              1 Reply Last reply
              0
              • H Offline
                H Offline
                hskoglund
                wrote on 11 Apr 2014, 14:37 last edited by
                #7

                Yeah I tried that but alas, if you look at "the docs":http://msdn.microsoft.com/en-us/library/aa373208.aspx, at the bottom (just before examples):
                "...
                This function does not stop the screen saver from executing. " :-(

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  MarianMMX
                  wrote on 12 Apr 2014, 09:11 last edited by
                  #8

                  Ok, tested on Windows XP:

                  DisableScreenSaver.c
                  @
                  #include <Windows.h>
                  #pragma comment(lib, "user32.lib")

                  int main()
                  {
                  SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, FALSE , NULL, SPIF_SENDWININICHANGE);
                  return 0;
                  }

                  @

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    maximus
                    wrote on 16 Dec 2014, 04:59 last edited by
                    #9

                    Finally trying to achieve this function after many user requested it.

                    I'm thinking of a easy cross-platform way to do it (Windows and Mac OS X)

                    What I'm thinking:

                    • start a QTimer that fire each 1 minute
                    • [Slot] emulate a key press on the keyboard or a small mouse movement (keyboard or mouse is not used in this part of the software so shouldn't matter)

                    I'll see if I can achieve it (not sure how to emulate key or mouse movement in Qt) and post result!

                    Thanks Qt!


                    Free Indoor Cycling Software - https://maximumtrainer.com

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      maximus
                      wrote on 16 Dec 2014, 04:59 last edited by
                      #10

                      Finally trying to achieve this function after many user requested it.

                      I'm thinking of a easy cross-platform way to do it (Windows and Mac OS X)

                      What I'm thinking:

                      • start a QTimer that fire each 1 minute
                      • [Slot] emulate a key press on the keyboard or a small mouse movement (keyboard or mouse is not used in this part of the software so shouldn't matter)

                      I'll see if I can achieve it (not sure how to emulate key or mouse movement in Qt) and post result!

                      Thanks Qt!


                      Free Indoor Cycling Software - https://maximumtrainer.com

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        maximus
                        wrote on 16 Dec 2014, 22:11 last edited by
                        #11

                        I tried this one on my QDialog. But still not working (screensaver activates)
                        Maybe QDialog doesn't handle KeyPress event? investigating..

                        @void WorkoutDialog::desactivateScreensaver() {

                        qDebug() << "OK DESACTIVATE SCREENSAVER!";
                        
                        QKeyEvent *event = new QKeyEvent(QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
                        QCoreApplication::postEvent(this, event);
                        
                        QKeyEvent *event1 = new QKeyEvent(QEvent::KeyRelease, Qt::Key_Enter, Qt::NoModifier);
                        QCoreApplication::postEvent(this, event1);
                        

                        }@


                        Free Indoor Cycling Software - https://maximumtrainer.com

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          maximus
                          wrote on 16 Dec 2014, 22:11 last edited by
                          #12

                          I tried this one on my QDialog. But still not working (screensaver activates)
                          Maybe QDialog doesn't handle KeyPress event? investigating..

                          @void WorkoutDialog::desactivateScreensaver() {

                          qDebug() << "OK DESACTIVATE SCREENSAVER!";
                          
                          QKeyEvent *event = new QKeyEvent(QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
                          QCoreApplication::postEvent(this, event);
                          
                          QKeyEvent *event1 = new QKeyEvent(QEvent::KeyRelease, Qt::Key_Enter, Qt::NoModifier);
                          QCoreApplication::postEvent(this, event1);
                          

                          }@


                          Free Indoor Cycling Software - https://maximumtrainer.com

                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            maximus
                            wrote on 17 Dec 2014, 00:04 last edited by
                            #13

                            Weird because I see my eventFilter being called, but the action never takes place on the UI (key pressed, mouseMove, ..)

                            @bool WorkoutDialog::eventFilter(QObject *watched, QEvent *event) {

                            Q_UNUSED(watched);
                            
                            //    qDebug() << "EventFilter " << watched << "Event:" << event;
                            
                            /// Disable enter pressed on dialog (bug cause it to minimize)
                            if(event->type() == QEvent::KeyPress) {
                                QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
                                if(keyEvent->key() == Qt::Key_Enter || keyEvent->key() == Qt::Key_Return )
                                    return true; // mark the event as handled
                            }
                            
                            
                            if(event->type() == QEvent::WindowStateChange)
                            {
                                if(windowState().testFlag(Qt::WindowMaximized) == true) {
                                    qDebug() << "Going call showFullSceen here";
                                    this->showFullScreenWin();
                                    return true;
                                }
                            }
                            return false;
                            

                            }@


                            Free Indoor Cycling Software - https://maximumtrainer.com

                            1 Reply Last reply
                            0
                            • M Offline
                              M Offline
                              maximus
                              wrote on 17 Dec 2014, 00:04 last edited by
                              #14

                              Weird because I see my eventFilter being called, but the action never takes place on the UI (key pressed, mouseMove, ..)

                              @bool WorkoutDialog::eventFilter(QObject *watched, QEvent *event) {

                              Q_UNUSED(watched);
                              
                              //    qDebug() << "EventFilter " << watched << "Event:" << event;
                              
                              /// Disable enter pressed on dialog (bug cause it to minimize)
                              if(event->type() == QEvent::KeyPress) {
                                  QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
                                  if(keyEvent->key() == Qt::Key_Enter || keyEvent->key() == Qt::Key_Return )
                                      return true; // mark the event as handled
                              }
                              
                              
                              if(event->type() == QEvent::WindowStateChange)
                              {
                                  if(windowState().testFlag(Qt::WindowMaximized) == true) {
                                      qDebug() << "Going call showFullSceen here";
                                      this->showFullScreenWin();
                                      return true;
                                  }
                              }
                              return false;
                              

                              }@


                              Free Indoor Cycling Software - https://maximumtrainer.com

                              1 Reply Last reply
                              0
                              • M Offline
                                M Offline
                                maximus
                                wrote on 17 Dec 2014, 00:59 last edited by
                                #15

                                [quote author="MarianMMX" date="1397293908"]Ok, tested on Windows XP:

                                DisableScreenSaver.c
                                @
                                #include <Windows.h>
                                #pragma comment(lib, "user32.lib")

                                int main()
                                {
                                SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, FALSE , NULL, SPIF_SENDWININICHANGE);
                                return 0;
                                }

                                @
                                [/quote]

                                Tested and works on windows 7 too. thanks
                                Update: not really what I wanted, it still disable the ScreenSaver after you leave the app.. don't want to mess with personal settings..

                                I gave up trying to emulate a key press, will use this code instead.
                                Trying to find an equivalent for Mac OS X now


                                Free Indoor Cycling Software - https://maximumtrainer.com

                                1 Reply Last reply
                                0
                                • M Offline
                                  M Offline
                                  maximus
                                  wrote on 17 Dec 2014, 00:59 last edited by
                                  #16

                                  [quote author="MarianMMX" date="1397293908"]Ok, tested on Windows XP:

                                  DisableScreenSaver.c
                                  @
                                  #include <Windows.h>
                                  #pragma comment(lib, "user32.lib")

                                  int main()
                                  {
                                  SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, FALSE , NULL, SPIF_SENDWININICHANGE);
                                  return 0;
                                  }

                                  @
                                  [/quote]

                                  Tested and works on windows 7 too. thanks
                                  Update: not really what I wanted, it still disable the ScreenSaver after you leave the app.. don't want to mess with personal settings..

                                  I gave up trying to emulate a key press, will use this code instead.
                                  Trying to find an equivalent for Mac OS X now


                                  Free Indoor Cycling Software - https://maximumtrainer.com

                                  1 Reply Last reply
                                  0
                                  • M Offline
                                    M Offline
                                    maximus
                                    wrote on 17 Dec 2014, 01:38 last edited by
                                    #17

                                    I read you can call Objective-C from Qt

                                    I want "to use this method":http://stackoverflow.com/questions/5596319/how-to-programmatically-prevent-a-mac-from-going-to-sleep/5596946#5596946

                                    I made a header file named "MacUtil.h" and a "MacUtil.mm" that will call the Objective-C code. Will see if I can get it to work and post result


                                    Free Indoor Cycling Software - https://maximumtrainer.com

                                    1 Reply Last reply
                                    0
                                    • M Offline
                                      M Offline
                                      maximus
                                      wrote on 17 Dec 2014, 01:38 last edited by
                                      #18

                                      I read you can call Objective-C from Qt

                                      I want "to use this method":http://stackoverflow.com/questions/5596319/how-to-programmatically-prevent-a-mac-from-going-to-sleep/5596946#5596946

                                      I made a header file named "MacUtil.h" and a "MacUtil.mm" that will call the Objective-C code. Will see if I can get it to work and post result


                                      Free Indoor Cycling Software - https://maximumtrainer.com

                                      1 Reply Last reply
                                      0
                                      • M Offline
                                        M Offline
                                        maximus
                                        wrote on 17 Dec 2014, 01:52 last edited by
                                        #19

                                        On the way, will edit this post with solution
                                        MacUtil.h

                                        @class MacUtil
                                        {

                                        public:
                                        MacUtil();
                                        ~MacUtil();

                                        void disableScreensaver();
                                        void releaseScreensaverLock();
                                        

                                        private:
                                        IOPMAssertionID assertionID;

                                        };

                                        #endif // MACUTIL_H@

                                        MacUtil.mm
                                        @//http://stackoverflow.com/questions/5596319/how-to-programmatically-prevent-a-mac-from-going-to-sleep/5596946#5596946
                                        void MacUtil::disableScreensaver() {

                                        CFStringRef* reasonForActivity= CFSTR("MaximumTrainer FullScreen Workout");
                                        IOReturn success = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoDisplaySleep,
                                                                            kIOPMAssertionLevelOn, reasonForActivity, &assertionID);
                                        if (success == kIOReturnSuccess)
                                        {
                                            //return sucess? todo..
                                        }
                                        

                                        }

                                        void MacUtil::releaseScreensaverLock() {

                                        IOPMAssertionRelease(assertionID);
                                        

                                        }
                                        @


                                        Free Indoor Cycling Software - https://maximumtrainer.com

                                        1 Reply Last reply
                                        0
                                        • M Offline
                                          M Offline
                                          maximus
                                          wrote on 17 Dec 2014, 01:52 last edited by
                                          #20

                                          On the way, will edit this post with solution
                                          MacUtil.h

                                          @class MacUtil
                                          {

                                          public:
                                          MacUtil();
                                          ~MacUtil();

                                          void disableScreensaver();
                                          void releaseScreensaverLock();
                                          

                                          private:
                                          IOPMAssertionID assertionID;

                                          };

                                          #endif // MACUTIL_H@

                                          MacUtil.mm
                                          @//http://stackoverflow.com/questions/5596319/how-to-programmatically-prevent-a-mac-from-going-to-sleep/5596946#5596946
                                          void MacUtil::disableScreensaver() {

                                          CFStringRef* reasonForActivity= CFSTR("MaximumTrainer FullScreen Workout");
                                          IOReturn success = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoDisplaySleep,
                                                                              kIOPMAssertionLevelOn, reasonForActivity, &assertionID);
                                          if (success == kIOReturnSuccess)
                                          {
                                              //return sucess? todo..
                                          }
                                          

                                          }

                                          void MacUtil::releaseScreensaverLock() {

                                          IOPMAssertionRelease(assertionID);
                                          

                                          }
                                          @


                                          Free Indoor Cycling Software - https://maximumtrainer.com

                                          1 Reply Last reply
                                          0

                                          • Login

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