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. Qt apps basic functionality
Forum Updated to NodeBB v4.3 + New Features

Qt apps basic functionality

Scheduled Pinned Locked Moved Solved General and Desktop
24 Posts 8 Posters 1.4k Views 4 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.
  • tomyT tomy

    Just reread my prior post!!
    My question is about QTimer mostly! I think I said that clearly.

    Let me ask that, another way: what is the difference between QTimer::singleShot(0, this, &Client::requestNewFortune); and calling requestNewFortune() directly without the timer?

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

    @tomy
    Hi
    Yes there is.
    The Qtimer puts on the event queue and will be called later meaning
    the execution continues to the next line.

    Calling the requestNewFortune will happen right now and the next line is not processed before this function ends.

    1 Reply Last reply
    2
    • tomyT tomy

      Just reread my prior post!!
      My question is about QTimer mostly! I think I said that clearly.

      Let me ask that, another way: what is the difference between QTimer::singleShot(0, this, &Client::requestNewFortune); and calling requestNewFortune() directly without the timer?

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by JKSH
      #16

      @tomy said in Qt apps basic functionality:

      what is the difference between QTimer::singleShot(0, this, &Client::requestNewFortune); and calling requestNewFortune() directly without the timer?

      Here is a simple illustration:

      void triggeredFunc()
      {
          qDebug("World");
      }
      
      void f1()
      {
          QTimer::singleShot(0, &triggeredFunc);
          qDebug("Hello");
      }
      
      void f2()
      {
          triggeredFunc();
          qDebug("Hello");
      }
      

      Assuming that your application has an event loop,

      • If you call f1(), your debug output will print "Hello" before "World".
      • If you call f2(), your debug output will print "World" before "Hello".

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      tomyT 1 Reply Last reply
      3
      • tomyT tomy

        Just reread my prior post!!
        My question is about QTimer mostly! I think I said that clearly.

        Let me ask that, another way: what is the difference between QTimer::singleShot(0, this, &Client::requestNewFortune); and calling requestNewFortune() directly without the timer?

        KroMignonK Offline
        KroMignonK Offline
        KroMignon
        wrote on last edited by
        #17

        @tomy As try to explain to you calling QTimer::singleShot(0, this, &Client::requestNewFortune); will post event into the QEventLoop of the QThread used by this.
        This means requestNewFortune() will be executed some time later, when all other previous event in the QEventLoop have been executed.
        This also ensure, requestNewFortune() will be executed in the thread on which this is running.
        And, to be exhaustive, requestNewFortune() will be executed, when attached Thread has an QEventQueue and the thread is running.

        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

        1 Reply Last reply
        2
        • JKSHJ JKSH

          @tomy said in Qt apps basic functionality:

          what is the difference between QTimer::singleShot(0, this, &Client::requestNewFortune); and calling requestNewFortune() directly without the timer?

          Here is a simple illustration:

          void triggeredFunc()
          {
              qDebug("World");
          }
          
          void f1()
          {
              QTimer::singleShot(0, &triggeredFunc);
              qDebug("Hello");
          }
          
          void f2()
          {
              triggeredFunc();
              qDebug("Hello");
          }
          

          Assuming that your application has an event loop,

          • If you call f1(), your debug output will print "Hello" before "World".
          • If you call f2(), your debug output will print "World" before "Hello".
          tomyT Offline
          tomyT Offline
          tomy
          wrote on last edited by
          #18

          @mrjj
          I'm confused by what you said.
          You firstly say QTimer will be called later. Then you say, calling the function will happen right now!

          @JKSH

          If you call f1(), your debug output will print "Hello" before "World".

          So, apparently my first assumption mentioned here more than one time was correct. That is, that QTimer postpones the execution of the method inside it (requestNewFortune), until all next lines inside the function (void Client::readFortune()), are executed. And then at the end when there is no line to execute, QTimer calls its function. Right?

          @KroMignon

          This means requestNewFortune() will be executed some time later, when all other previous event in the QEventLoop have been executed.

          This is on the contrary to the code @JKSH offered above, because there, the function inside the QTimer is called when all other next lines are called.

          The subject presumably is not that convoluted but I don't know why I understand your posts differently! :(

          KroMignonK JKSHJ 2 Replies Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #19

            @tomy said in Qt apps basic functionality:

            You firstly say QTimer will be called later. Then you say, calling the function will happen right now!

            Ok let me make it clear.

            QTimer::singleShot(0, this, &Client::requestNewFortune); ---> later called


            requestNewFortune(); ---> called right here. right now.

            And in this context. Later means when event loop gets around to it.

            1 Reply Last reply
            0
            • tomyT tomy

              @mrjj
              I'm confused by what you said.
              You firstly say QTimer will be called later. Then you say, calling the function will happen right now!

              @JKSH

              If you call f1(), your debug output will print "Hello" before "World".

              So, apparently my first assumption mentioned here more than one time was correct. That is, that QTimer postpones the execution of the method inside it (requestNewFortune), until all next lines inside the function (void Client::readFortune()), are executed. And then at the end when there is no line to execute, QTimer calls its function. Right?

              @KroMignon

              This means requestNewFortune() will be executed some time later, when all other previous event in the QEventLoop have been executed.

              This is on the contrary to the code @JKSH offered above, because there, the function inside the QTimer is called when all other next lines are called.

              The subject presumably is not that convoluted but I don't know why I understand your posts differently! :(

              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by KroMignon
              #20

              @tomy said in Qt apps basic functionality:

              This is on the contrary to the code @JKSH offered above, because there, the function inside the QTimer is called when all other next lines are called.
              The subject presumably is not that convoluted but I don't know why I understand your posts differently! :(

              Dear @tomy, it seems my explanation are not that clear as I supposed. So I do another try:

              1. QThread owns a QEventLoop
              2. When starting a QThread, the thread will wait until a new event is stored in the event loop, take it and execute the function with the attached parameters
              3. When calling QTimer::singleShot(0, this, &Client::requestNewFortune);, what happen is that a temporary QTimer instance is created and started. When the passed delay is elapsed (in this case 0 milliseconds), the function/slots (in this case &Client::requestNewFortune) is stored in the event loop from the thread used by the receiver QObject instance (in this case this)

              I think I can't explain it more clearly and hope now you have understand the difference between QTimer::singleShot(0, this, &Client::requestNewFortune); and calling directly requestNewFortune().

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              1 Reply Last reply
              1
              • tomyT tomy

                @mrjj
                I'm confused by what you said.
                You firstly say QTimer will be called later. Then you say, calling the function will happen right now!

                @JKSH

                If you call f1(), your debug output will print "Hello" before "World".

                So, apparently my first assumption mentioned here more than one time was correct. That is, that QTimer postpones the execution of the method inside it (requestNewFortune), until all next lines inside the function (void Client::readFortune()), are executed. And then at the end when there is no line to execute, QTimer calls its function. Right?

                @KroMignon

                This means requestNewFortune() will be executed some time later, when all other previous event in the QEventLoop have been executed.

                This is on the contrary to the code @JKSH offered above, because there, the function inside the QTimer is called when all other next lines are called.

                The subject presumably is not that convoluted but I don't know why I understand your posts differently! :(

                JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by JKSH
                #21

                @tomy said in Qt apps basic functionality:

                QTimer postpones the execution of the method inside it (requestNewFortune), until all next lines inside the function (void Client::readFortune()), are executed. And then at the end when there is no line to execute, QTimer calls its function. Right?

                Almost right.

                QTimer does not call requestNewFortune(). QTimer puts requestNewFortune() in the event loop.

                The event loop calls the requestNewFortune() after readFortune() finishes all its lines and returns.

                @tomy said in Qt apps basic functionality:

                This means requestNewFortune() will be executed some time later, when all other previous event in the QEventLoop have been executed.

                This is on the contrary to the code @JKSH offered above, because there, the function inside the QTimer is called when all other next lines are called.

                @KroMignon's description matches the code that I wrote. It is not contrary.

                The sequence is this:

                1. The event loop calls f1(). f1() starts running, so it blocks the event loop.
                2. The QTimer in f1() puts triggeredFunc() in the event loop queue.
                3. f1() prints "Hello".
                4. f1() returns. The event loop can now proceed to the next task.
                5. The event loop checks its queue. It sees triggeredFunc() in the queue, so the event loop calls triggeredFunc(). triggeredFunc() starts running, so it blocks the event loop.
                6. triggeredFunc() prints "World".
                7. triggeredFunc() returns. The event loop can now proceed to the next task.
                8. The event loop checks its queue. If there is nothing in the queue, the event loop waits for the next event.

                With f2(), the sequence is this:

                1. The event loop calls f2(). f2() starts running, so it blocks the event loop.
                2. f2() calls triggeredFunc() directly. triggeredFunc() starts running, so it blocks f2().
                3. triggeredFunc() prints "World".
                4. triggeredFunc() returns. f2() can now proceed to the next line.
                5. f2() prints "World".
                6. f2() returns. The event loop can now proceed to the next task.
                7. The event loop checks its queue. If there is nothing in the queue, the event loop waits for the next event.

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                tomyT 1 Reply Last reply
                4
                • JKSHJ JKSH

                  @tomy said in Qt apps basic functionality:

                  QTimer postpones the execution of the method inside it (requestNewFortune), until all next lines inside the function (void Client::readFortune()), are executed. And then at the end when there is no line to execute, QTimer calls its function. Right?

                  Almost right.

                  QTimer does not call requestNewFortune(). QTimer puts requestNewFortune() in the event loop.

                  The event loop calls the requestNewFortune() after readFortune() finishes all its lines and returns.

                  @tomy said in Qt apps basic functionality:

                  This means requestNewFortune() will be executed some time later, when all other previous event in the QEventLoop have been executed.

                  This is on the contrary to the code @JKSH offered above, because there, the function inside the QTimer is called when all other next lines are called.

                  @KroMignon's description matches the code that I wrote. It is not contrary.

                  The sequence is this:

                  1. The event loop calls f1(). f1() starts running, so it blocks the event loop.
                  2. The QTimer in f1() puts triggeredFunc() in the event loop queue.
                  3. f1() prints "Hello".
                  4. f1() returns. The event loop can now proceed to the next task.
                  5. The event loop checks its queue. It sees triggeredFunc() in the queue, so the event loop calls triggeredFunc(). triggeredFunc() starts running, so it blocks the event loop.
                  6. triggeredFunc() prints "World".
                  7. triggeredFunc() returns. The event loop can now proceed to the next task.
                  8. The event loop checks its queue. If there is nothing in the queue, the event loop waits for the next event.

                  With f2(), the sequence is this:

                  1. The event loop calls f2(). f2() starts running, so it blocks the event loop.
                  2. f2() calls triggeredFunc() directly. triggeredFunc() starts running, so it blocks f2().
                  3. triggeredFunc() prints "World".
                  4. triggeredFunc() returns. f2() can now proceed to the next line.
                  5. f2() prints "World".
                  6. f2() returns. The event loop can now proceed to the next task.
                  7. The event loop checks its queue. If there is nothing in the queue, the event loop waits for the next event.
                  tomyT Offline
                  tomyT Offline
                  tomy
                  wrote on last edited by
                  #22

                  @JKSH

                  This is a clear answer, explained step-by-step and omits nothing for ambiguity. Thanks.
                  I also must thank other members, who dedicated time to help me. thanks.
                  The thread is closed and the issue is solved. :)

                  ODБOïO JKSHJ 2 Replies Last reply
                  1
                  • tomyT tomy

                    @JKSH

                    This is a clear answer, explained step-by-step and omits nothing for ambiguity. Thanks.
                    I also must thank other members, who dedicated time to help me. thanks.
                    The thread is closed and the issue is solved. :)

                    ODБOïO Offline
                    ODБOïO Offline
                    ODБOï
                    wrote on last edited by
                    #23
                    This post is deleted!
                    1 Reply Last reply
                    0
                    • tomyT tomy

                      @JKSH

                      This is a clear answer, explained step-by-step and omits nothing for ambiguity. Thanks.
                      I also must thank other members, who dedicated time to help me. thanks.
                      The thread is closed and the issue is solved. :)

                      JKSHJ Offline
                      JKSHJ Offline
                      JKSH
                      Moderators
                      wrote on last edited by
                      #24

                      @tomy You're most welcome. Happy coding!

                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                      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