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. Infinite condition checking
Qt 6.12 LTS is out! See what's new in the release blog

Infinite condition checking

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 4 Posters 3.4k Views 3 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.
  • H Offline
    H Offline
    hbatalha
    wrote on last edited by
    #1

    I have a task where a function is called and the first thing done in that is to call a bool function to check a condition before proceeding. If that condition is true the code proceeds and if not it returns.

    But I need a way to periodically check that condition, if it ever returns that function will be called again.

    bool condition()
    {
    ...
        //return true or false;
    }
    
    void foo()
    {
        if( ! condition)
            return;
    
    ...
    
    }
    
    

    I need to find a way to go back to foo in case condition ever returns true.

    I thought about QEventLoop but I want more options.

    Any help is appreciated. Thanks in advance!

    raven-worxR 1 Reply Last reply
    0
    • H hbatalha

      I have a task where a function is called and the first thing done in that is to call a bool function to check a condition before proceeding. If that condition is true the code proceeds and if not it returns.

      But I need a way to periodically check that condition, if it ever returns that function will be called again.

      bool condition()
      {
      ...
          //return true or false;
      }
      
      void foo()
      {
          if( ! condition)
              return;
      
      ...
      
      }
      
      

      I need to find a way to go back to foo in case condition ever returns true.

      I thought about QEventLoop but I want more options.

      Any help is appreciated. Thanks in advance!

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @hbatalha said in Infinite condition checking:

      I thought about QEventLoop but I want more options.

      elaborate

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      H 1 Reply Last reply
      0
      • raven-worxR raven-worx

        @hbatalha said in Infinite condition checking:

        I thought about QEventLoop but I want more options.

        elaborate

        H Offline
        H Offline
        hbatalha
        wrote on last edited by
        #3

        @raven-worx I thought about creating a loop that would run indefinitely until the condition returns true, if it ever does, meanwhile the user can use the application "normally".
        But I don't want to use QEventLoop , because it adds an extra complexity to the program. That's why I want more options.

        raven-worxR 1 Reply Last reply
        0
        • H hbatalha

          @raven-worx I thought about creating a loop that would run indefinitely until the condition returns true, if it ever does, meanwhile the user can use the application "normally".
          But I don't want to use QEventLoop , because it adds an extra complexity to the program. That's why I want more options.

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @hbatalha
          but the extra logic is needed, since you want to block the execution but actually you do not want to.
          You can use WorkerScripts in QML which act like threads.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          H 1 Reply Last reply
          1
          • raven-worxR raven-worx

            @hbatalha
            but the extra logic is needed, since you want to block the execution but actually you do not want to.
            You can use WorkerScripts in QML which act like threads.

            H Offline
            H Offline
            hbatalha
            wrote on last edited by
            #5

            @raven-worx I am not using QML

            but the extra logic is needed

            Is there another way to do this?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              Since foo checks the condition on each call, in fact, you want something that calls regularly foo ?

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

              H 1 Reply Last reply
              0
              • SGaistS SGaist

                Hi,

                Since foo checks the condition on each call, in fact, you want something that calls regularly foo ?

                H Offline
                H Offline
                hbatalha
                wrote on last edited by
                #7

                @SGaist something that calls condition regularly and if condition returns true then foo will be called

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Sounds like a simple QTimer that calls foo regularly should do the job.

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

                  H 1 Reply Last reply
                  3
                  • SGaistS SGaist

                    Sounds like a simple QTimer that calls foo regularly should do the job.

                    H Offline
                    H Offline
                    hbatalha
                    wrote on last edited by
                    #9

                    @SGaist That did solve my problem, thanks

                    void SomeClass::foo()
                    {
                        if( ! condition)
                        {
                            QTimer::singleShot(5000, this, &SomeClass::foo);
                    
                            return;
                        }
                    ...
                    
                    }
                    
                    1 Reply Last reply
                    2
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      You might want to consider making a QTimer member variable to be able to stop it for example when your application ends.

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

                      H 1 Reply Last reply
                      0
                      • SGaistS SGaist

                        You might want to consider making a QTimer member variable to be able to stop it for example when your application ends.

                        H Offline
                        H Offline
                        hbatalha
                        wrote on last edited by
                        #11

                        @SGaist didn't quite understand what you meant, when my application won't everything be stopped?

                        Also, I need to call foo only once when the timeout is emitted, took that example from the documentation.

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          I don't know how long foo will take so you might not want for it to triggered when you end your application. Ending an application does not mean everything is destroyed instantly.

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

                          H 2 Replies Last reply
                          0
                          • SGaistS SGaist

                            I don't know how long foo will take so you might not want for it to triggered when you end your application. Ending an application does not mean everything is destroyed instantly.

                            H Offline
                            H Offline
                            hbatalha
                            wrote on last edited by
                            #13

                            @SGaist foo is not from the mainwindow, is part of another class, should I call stop in the destructor?

                            jsulmJ 1 Reply Last reply
                            0
                            • H hbatalha

                              @SGaist foo is not from the mainwindow, is part of another class, should I call stop in the destructor?

                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @hbatalha said in Infinite condition checking:

                              is part of another class

                              Then put it there as member variable and stop it in destructor (or whenever needed).

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              H 1 Reply Last reply
                              0
                              • jsulmJ jsulm

                                @hbatalha said in Infinite condition checking:

                                is part of another class

                                Then put it there as member variable and stop it in destructor (or whenever needed).

                                H Offline
                                H Offline
                                hbatalha
                                wrote on last edited by
                                #15

                                @jsulm ok thanks

                                1 Reply Last reply
                                0
                                • SGaistS SGaist

                                  I don't know how long foo will take so you might not want for it to triggered when you end your application. Ending an application does not mean everything is destroyed instantly.

                                  H Offline
                                  H Offline
                                  hbatalha
                                  wrote on last edited by
                                  #16

                                  @SGaist @jsulm

                                  Ending an application does not mean everything is destroyed instantly.

                                  I have followed your suggestion, and now I am wondering, is this something that I have to always watch out for, is there a name for this kind of situation, a reference I can search?

                                  And also, why is everything not destroyed instantly? What are the steps being taken? What is happening under the hood there? Any reference?

                                  1 Reply Last reply
                                  0
                                  • SGaistS Offline
                                    SGaistS Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

                                    Order of destruction, code run during destruction, etc

                                    Think of a database, you likely appreciate the fact that all operations are finalised before it stops so you don't have corrupted data when you restart it.

                                    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
                                    1

                                    • Login

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