Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Set Delay Timer
Qt 6.12 LTS is out! See what's new in the release blog

Set Delay Timer

Scheduled Pinned Locked Moved Solved General and Desktop
26 Posts 8 Posters 22.9k Views 2 Watching
  • 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.
  • Y Yrandom

    This post is deleted!

    VRoninV Offline
    VRoninV Offline
    VRonin
    wrote on last edited by
    #10

    @Yrandom said in Set Delay Timer:

    VerschiebeSpiel.cpp.cc: In member function 'void erschiebeSpiel::buttonClicked(QWidget*)':

    That's not where you should put the connect. my code snippet should go just after you create the button (in the constructor?)

    while( QTime::currentTime() < dieTime )
    {
    QCoreApplication::processEvents( QEventLoop::AllEvents, 100 );
    }

    This is just bad design. you are blocking the main thread and forcing the event loop to kinda keep the UI responsive. Nasty

    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
    ~Napoleon Bonaparte

    On a crusade to banish setIndexWidget() from the holy land of Qt

    1 Reply Last reply
    1
    • Y Yrandom

      @J.Hilk

      thx for this example
      it works great.

      also just mention: i found out other solution for that

      using processevents and currenttime
      but i want to unterstand how this part work

      QCoreApplication::processEvents( QEventLoop::AllEvents, 100 );
      

      What is the 100(maxtime) mean ? because i just need to process the Event again (Loop all Event) and the current time will increase automatically.
      I dont need to put 100 as Parameter and it still work

      Thx all for the answer

      
      // Delay time for button color
      void ss::delay(int millisecondsToWait )
      {
          QTime dieTime = QTime::currentTime().addMSecs( millisecondsToWait );
          while( QTime::currentTime() < dieTime )
          {
              QCoreApplication::processEvents( QEventLoop::AllEvents, 100 );
          }
      }
      
      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #11

      @Yrandom

      Usually during a while loop, all other tasks are suspendet until the loop or more precisely the function returns. That includes the paint/repaint event that is triggered by setStyleSheet().

      QCoreApplication::processEvents() is an uncomely way to tell your app, "That with the function, do whatever else is qued".
      The Int value you pass to processEvents is a max value in -I believe -ms, you give other Processes to finish their stuff, after that time your code after the call gets processed again.

      a While loop now is calling that every eventloop-cycle

      My advice, don't use it, if you have other options.


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      1
      • M Offline
        M Offline
        mpergand
        wrote on last edited by
        #12

        A wait function like this:

        void wait(float sec)
        {
            QElapsedTimer timer;
            timer.start();
        
            while(! timer.hasExpired(sec*1000))
                QCoreApplication::processEvents();
        }
        

        allows you to simpy do that:

        b->setStyleSheet("background-color:green");
        wait(0.3);
        b->setStyleSheet("background-color:red");
        

        Pretty and clean isn't it ?

        J.HilkJ VRoninV 2 Replies Last reply
        0
        • M mpergand

          A wait function like this:

          void wait(float sec)
          {
              QElapsedTimer timer;
              timer.start();
          
              while(! timer.hasExpired(sec*1000))
                  QCoreApplication::processEvents();
          }
          

          allows you to simpy do that:

          b->setStyleSheet("background-color:green");
          wait(0.3);
          b->setStyleSheet("background-color:red");
          

          Pretty and clean isn't it ?

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #13

          @mpergand said in Set Delay Timer:

          while(! timer.hasExpired(sec*1000))
              QCoreApplication::processEvents();
          

          Pretty and clean isn't it ?

          No, it's not!

          Do not use QCoreApplication::processEvents if you can avoid it!


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          0
          • M mpergand

            A wait function like this:

            void wait(float sec)
            {
                QElapsedTimer timer;
                timer.start();
            
                while(! timer.hasExpired(sec*1000))
                    QCoreApplication::processEvents();
            }
            

            allows you to simpy do that:

            b->setStyleSheet("background-color:green");
            wait(0.3);
            b->setStyleSheet("background-color:red");
            

            Pretty and clean isn't it ?

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #14

            @mpergand said in Set Delay Timer:

            Pretty and clean isn't it ?

            No it is not. it uses 75% of one of my CPU cores for absolutely nothing

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            M 1 Reply Last reply
            0
            • M Offline
              M Offline
              mpergand
              wrote on last edited by mpergand
              #15

              @j.hilk Do not use QCoreApplication::processEvents if you can avoid it!

              Why ?
              Explain any good reason to avoid it.

              J.HilkJ 1 Reply Last reply
              0
              • M mpergand

                @j.hilk Do not use QCoreApplication::processEvents if you can avoid it!

                Why ?
                Explain any good reason to avoid it.

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by J.Hilk
                #16

                @mpergand said in Set Delay Timer:

                @j.hilk Do not use QCoreApplication::processEvents if you can avoid it!

                Why ?
                Explain any good reason to avoid it.

                At least used in such a loop->
                @VRonin said in Set Delay Timer:

                No it is not. it uses 75% of one of my CPU cores for absolutely nothing

                Its also contrarian to the event-driven programming approach


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                0
                • VRoninV VRonin

                  @mpergand said in Set Delay Timer:

                  Pretty and clean isn't it ?

                  No it is not. it uses 75% of one of my CPU cores for absolutely nothing

                  M Offline
                  M Offline
                  mpergand
                  wrote on last edited by
                  #17

                  @VRonin said in Set Delay Timer:

                  No it is not. it uses 75% of one of my CPU cores for absolutely nothing

                  For .3 sec not a big deal.
                  I agree, that it's should by used for short period only (< 1 sec)

                  The main drawback I know (i use the same principle on MacOS) is that it can leading to some reentrant call in some cases.

                  VRoninV 1 Reply Last reply
                  0
                  • M mpergand

                    @VRonin said in Set Delay Timer:

                    No it is not. it uses 75% of one of my CPU cores for absolutely nothing

                    For .3 sec not a big deal.
                    I agree, that it's should by used for short period only (< 1 sec)

                    The main drawback I know (i use the same principle on MacOS) is that it can leading to some reentrant call in some cases.

                    VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by
                    #18

                    @mpergand said in Set Delay Timer:

                    For .3 sec not a big deal.

                    It is. especially on mobile/embedded hardware. it's just bad design especially when alternatives based of QEventLoop exist (but of course use the async is ALWAYS the best design)

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    M 1 Reply Last reply
                    3
                    • VRoninV VRonin

                      @mpergand said in Set Delay Timer:

                      For .3 sec not a big deal.

                      It is. especially on mobile/embedded hardware. it's just bad design especially when alternatives based of QEventLoop exist (but of course use the async is ALWAYS the best design)

                      M Offline
                      M Offline
                      mpergand
                      wrote on last edited by
                      #19

                      @VRonin said in Set Delay Timer:

                      It is. especially on mobile/embedded hardware. it's just bad design especially when alternatives based of QEventLoop exist (but of course use the async is ALWAYS the best design)

                      Ok, add a small QThread::sleep should do the job as well.

                      Please, can you post an example of a wait function with a QEventLoop instead.

                      1 Reply Last reply
                      0
                      • VRoninV Offline
                        VRoninV Offline
                        VRonin
                        wrote on last edited by VRonin
                        #20

                        add a small QThread::sleep should do the job as well

                        it does not, reason in link below

                        can you post an example of a wait function with a QEventLoop instead.

                        https://forum.qt.io/topic/78715/why-my-timer-is-not-emit-timeout-signal

                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                        ~Napoleon Bonaparte

                        On a crusade to banish setIndexWidget() from the holy land of Qt

                        1 Reply Last reply
                        0
                        • Y Offline
                          Y Offline
                          Yrandom
                          wrote on last edited by Yrandom
                          #21

                          WOW. All the discussion :)
                          First off all, thx for the answer.
                          I have one more question to fullfill my Project. I dont want to start new Topic.

                          I have a class GameKonfiguration
                          and class MainWindow
                          and the main()

                          #include "Konfiguration.h"
                          #include "MainWindow.h"
                              
                          int main(int argc, char *argv[]) {
                              // initialize resources, if needed
                              // Q_INIT_RESOURCE(resfile);
                          
                              QApplication app(argc, argv);
                              
                              MainWindow *window = new MainWindow(&app);
                              window->show();
                          
                              Konfiguration *konfiguration = new Konfiguration(&app,window);
                              konfiguration->show();
                          
                              return app.exec();
                          }
                          

                          in main

                          Now I make this Programm so far, that when i start, both Windows open and i can Change configuration and the gamefield show and user can Play and actually i can quit the game via configuration. everything fine

                          Now I want to Combine the menu bar from mainwindow and use the menu bar to open my configuration window.
                          However the Problem i have is now, i cant include Header a in Header b and Header b in Header a.

                          // mainwindow Header file
                          #include "ui_MainWindow.h"
                          #include <QApplication>
                          //some Header here
                          
                          //try
                          #include "Konfiguration.h"
                          //-- not work because in Konfiguration.h i already include mainwindow.h in oder to use for the Slot etc
                          
                          
                          using namespace std;
                          
                          class MainWindow : public QMainWindow {
                             Q_OBJECT
                          private:
                              Ui::MainWindow widget;
                              QApplication *app;
                              // Try
                          Konfiguration *confi;
                              
                          public:
                              MainWindow(QApplication*);
                            //some function
                              
                          signals:
                              public slots:
                                // function etc ........
                                 void openConfi();
                          };
                          
                           
                          
                          
                          // in mainwindow.cpp
                          // Open Game Configuration
                              QObject::connect(widget.SpielKonfiguration,SIGNAL(triggered()),this,SLOT(openConfi()));
                              
                              // End Game
                              QObject::connect(widget.SpielBeenden,SIGNAL(triggered()),app,SLOT(quit()));
                          
                           // Configuration Window
                          void MainWindow::openConfi(){
                              // in this Slot , the configuration window should open
                          confi->show();
                          }
                          
                          

                          Konfiguration.h and cpp

                          #ifndef KONFIGURATION_H
                          #define KONFIGURATION_H
                          
                          
                          #include <QApplication>
                          #include "MainWindow.h"
                          // some Header here....
                          
                          class Konfiguration : public QDialog{
                              Q_OBJECT
                              
                          private:
                              QApplication *app;
                            // here i create mainwindow pointer to Setup the gamefield
                              MainWindow *spielFenster;    
                                 //some code here
                          public:
                              Konfiguration(QApplication*, MainWindow*);
                              virtual ~Konfiguration();     
                          
                              public slots:
                                  void clearText();
                          //      void updateTextN(QString);
                          //      void updateTextM(QString);
                          };
                          
                          
                          //konfiguration.cpp
                          
                          // use the mainwindow pointer to set Slot 
                          
                            //Set N, M in VerschiebeSpiel
                           QObject::connect(editN,SIGNAL(textChanged(QString)),spielFenster,SLOT(setN(QString)));
                           QObject::connect(editM,SIGNAL(textChanged(QString)),spielFenster,SLOT(setM(QString)));
                          
                          
                          

                          So how can I open the Konfiguration Window via the Menu bar of Mainwindow but I can still use the Konfiguration to configure the MainWindow ?

                          Muss be simple for you guys, but quite hard for newbie :)

                          thx for your help

                          mrjjM 1 Reply Last reply
                          0
                          • Y Yrandom

                            WOW. All the discussion :)
                            First off all, thx for the answer.
                            I have one more question to fullfill my Project. I dont want to start new Topic.

                            I have a class GameKonfiguration
                            and class MainWindow
                            and the main()

                            #include "Konfiguration.h"
                            #include "MainWindow.h"
                                
                            int main(int argc, char *argv[]) {
                                // initialize resources, if needed
                                // Q_INIT_RESOURCE(resfile);
                            
                                QApplication app(argc, argv);
                                
                                MainWindow *window = new MainWindow(&app);
                                window->show();
                            
                                Konfiguration *konfiguration = new Konfiguration(&app,window);
                                konfiguration->show();
                            
                                return app.exec();
                            }
                            

                            in main

                            Now I make this Programm so far, that when i start, both Windows open and i can Change configuration and the gamefield show and user can Play and actually i can quit the game via configuration. everything fine

                            Now I want to Combine the menu bar from mainwindow and use the menu bar to open my configuration window.
                            However the Problem i have is now, i cant include Header a in Header b and Header b in Header a.

                            // mainwindow Header file
                            #include "ui_MainWindow.h"
                            #include <QApplication>
                            //some Header here
                            
                            //try
                            #include "Konfiguration.h"
                            //-- not work because in Konfiguration.h i already include mainwindow.h in oder to use for the Slot etc
                            
                            
                            using namespace std;
                            
                            class MainWindow : public QMainWindow {
                               Q_OBJECT
                            private:
                                Ui::MainWindow widget;
                                QApplication *app;
                                // Try
                            Konfiguration *confi;
                                
                            public:
                                MainWindow(QApplication*);
                              //some function
                                
                            signals:
                                public slots:
                                  // function etc ........
                                   void openConfi();
                            };
                            
                             
                            
                            
                            // in mainwindow.cpp
                            // Open Game Configuration
                                QObject::connect(widget.SpielKonfiguration,SIGNAL(triggered()),this,SLOT(openConfi()));
                                
                                // End Game
                                QObject::connect(widget.SpielBeenden,SIGNAL(triggered()),app,SLOT(quit()));
                            
                             // Configuration Window
                            void MainWindow::openConfi(){
                                // in this Slot , the configuration window should open
                            confi->show();
                            }
                            
                            

                            Konfiguration.h and cpp

                            #ifndef KONFIGURATION_H
                            #define KONFIGURATION_H
                            
                            
                            #include <QApplication>
                            #include "MainWindow.h"
                            // some Header here....
                            
                            class Konfiguration : public QDialog{
                                Q_OBJECT
                                
                            private:
                                QApplication *app;
                              // here i create mainwindow pointer to Setup the gamefield
                                MainWindow *spielFenster;    
                                   //some code here
                            public:
                                Konfiguration(QApplication*, MainWindow*);
                                virtual ~Konfiguration();     
                            
                                public slots:
                                    void clearText();
                            //      void updateTextN(QString);
                            //      void updateTextM(QString);
                            };
                            
                            
                            //konfiguration.cpp
                            
                            // use the mainwindow pointer to set Slot 
                            
                              //Set N, M in VerschiebeSpiel
                             QObject::connect(editN,SIGNAL(textChanged(QString)),spielFenster,SLOT(setN(QString)));
                             QObject::connect(editM,SIGNAL(textChanged(QString)),spielFenster,SLOT(setM(QString)));
                            
                            
                            

                            So how can I open the Konfiguration Window via the Menu bar of Mainwindow but I can still use the Konfiguration to configure the MainWindow ?

                            Muss be simple for you guys, but quite hard for newbie :)

                            thx for your help

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by mrjj
                            #22

                            @Yrandom

                            the circular includes problem can often be solved with a forward of the class

                            so instead of ( in the other class header)
                            #include <myclass>

                            you just
                            do

                            class MyClass; // this is a forward

                            and you are allowed to have
                            class Other {
                            MyClass * mc;

                            Since all the use of the Myclass functions, are in the CPP file, it works.

                            In the CPP file you then include the actual header.

                            Not sure i explained it good :(

                            Please see
                            http://stackoverflow.com/questions/625799/resolve-header-include-circular-dependencies

                            Y 1 Reply Last reply
                            2
                            • mrjjM mrjj

                              @Yrandom

                              the circular includes problem can often be solved with a forward of the class

                              so instead of ( in the other class header)
                              #include <myclass>

                              you just
                              do

                              class MyClass; // this is a forward

                              and you are allowed to have
                              class Other {
                              MyClass * mc;

                              Since all the use of the Myclass functions, are in the CPP file, it works.

                              In the CPP file you then include the actual header.

                              Not sure i explained it good :(

                              Please see
                              http://stackoverflow.com/questions/625799/resolve-header-include-circular-dependencies

                              Y Offline
                              Y Offline
                              Yrandom
                              wrote on last edited by
                              #23

                              @mrjj
                              thx for the quick answer.

                              Now I got build successfull but when I start the Programm and klick on menubar set configuration
                              I got Windows disconnection and have to quit

                              here is what i Changed

                              ///////////////////////////////
                              // in mainwindow.h
                              // mainclass mainwindow
                              #include <QMenuBar>
                              #include <QAction>
                              
                              class Konfiguration; // this is the other class
                              
                              using namespace std;
                              
                              class MainWindow : public QMainWindow {
                                 Q_OBJECT
                              private:
                              Konfiguration *confi;
                              //code
                              }
                              
                              //////////////////////////
                              //mainwindow.cpp
                              #include "mainwindow.h"
                              #include "Konfiguration.h"
                              
                               // Configuration Window
                              void MainWindow::openConfi(){
                              //
                              // try to add something like confi = new Konfiguration(&app,this) like in the main() is not working either.
                              //
                                  confi->show();
                              // Debugger stoppt at show()
                              }
                              
                              // in Konfiguration.h
                              #include "MainWindow.h"
                              
                              class Konfiguration : public QDialog{
                                  Q_OBJECT
                                  
                              private:
                              code
                              }
                              
                              
                              
                              
                              mrjjM 1 Reply Last reply
                              0
                              • Y Yrandom

                                @mrjj
                                thx for the quick answer.

                                Now I got build successfull but when I start the Programm and klick on menubar set configuration
                                I got Windows disconnection and have to quit

                                here is what i Changed

                                ///////////////////////////////
                                // in mainwindow.h
                                // mainclass mainwindow
                                #include <QMenuBar>
                                #include <QAction>
                                
                                class Konfiguration; // this is the other class
                                
                                using namespace std;
                                
                                class MainWindow : public QMainWindow {
                                   Q_OBJECT
                                private:
                                Konfiguration *confi;
                                //code
                                }
                                
                                //////////////////////////
                                //mainwindow.cpp
                                #include "mainwindow.h"
                                #include "Konfiguration.h"
                                
                                 // Configuration Window
                                void MainWindow::openConfi(){
                                //
                                // try to add something like confi = new Konfiguration(&app,this) like in the main() is not working either.
                                //
                                    confi->show();
                                // Debugger stoppt at show()
                                }
                                
                                // in Konfiguration.h
                                #include "MainWindow.h"
                                
                                class Konfiguration : public QDialog{
                                    Q_OBJECT
                                    
                                private:
                                code
                                }
                                
                                
                                
                                
                                mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by
                                #24

                                @Yrandom said in Set Delay Timer:

                                Konfiguration *confi;

                                Did you remeber to new it ?

                                Konfiguration *confi; is just a pointer :)

                                you MUST do
                                confi = new Konfiguration()

                                before using it for anything

                                Y 1 Reply Last reply
                                1
                                • mrjjM mrjj

                                  @Yrandom said in Set Delay Timer:

                                  Konfiguration *confi;

                                  Did you remeber to new it ?

                                  Konfiguration *confi; is just a pointer :)

                                  you MUST do
                                  confi = new Konfiguration()

                                  before using it for anything

                                  Y Offline
                                  Y Offline
                                  Yrandom
                                  wrote on last edited by
                                  #25

                                  @mrjj

                                  :D my bad
                                  i did that but i pas the function by reference

                                  void MainWindow::openConfi(){
                                  //
                                   //try to add something like confi = new Konfiguration(&app,this) like in the main() is not working either.
                                  // pass by reference mistake :D
                                      confi->show();
                                  // Debugger stoppt at show()
                                  }
                                  
                                  //
                                  // IT work now. :D
                                  //
                                  
                                  
                                  1 Reply Last reply
                                  0
                                  • M Offline
                                    M Offline
                                    mchinand
                                    wrote on last edited by
                                    #26

                                    I wonder if this could be achieved using Property Animation instead of using timers and stylesheet manipulation.

                                    1 Reply Last reply
                                    2

                                    • Login

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