Defer a function call in Qt












2















I have a function in Qt 4.8 (windows), which has 3 things to do, like this:



void f()
{
//1. Do initialization

//2. Defer g() for next event (on the same thread) so that it may execute after f() is over

//3. Call h() which is time consuming
}


Now, I am not sure how to implement the 2nd step i.e. how to ensure that g() is called after the call to f() is complete, on the same thread (but on the different stack). I tried using QTimer but if I provide a short time say 10 ms, the function g() is called even when f() has not completed execution. So, relying on timers does not seem to be a reliable idea. Please help, any ideas are much appreciated.










share|improve this question























  • What does this mean on the same thread (but on the different stack)?

    – Alex F
    Jun 18 '14 at 14:01











  • What prevents you to call g() after f() and not g() from f()?

    – vahancho
    Jun 18 '14 at 14:11











  • So, I want g() to be called from the next event stack of message loop which means that instead of calling g() at this time, I just want to post an event in message loop and call that later when current event processing is complete. I hope it clarifies the things.

    – Aarkan
    Jun 18 '14 at 14:32
















2















I have a function in Qt 4.8 (windows), which has 3 things to do, like this:



void f()
{
//1. Do initialization

//2. Defer g() for next event (on the same thread) so that it may execute after f() is over

//3. Call h() which is time consuming
}


Now, I am not sure how to implement the 2nd step i.e. how to ensure that g() is called after the call to f() is complete, on the same thread (but on the different stack). I tried using QTimer but if I provide a short time say 10 ms, the function g() is called even when f() has not completed execution. So, relying on timers does not seem to be a reliable idea. Please help, any ideas are much appreciated.










share|improve this question























  • What does this mean on the same thread (but on the different stack)?

    – Alex F
    Jun 18 '14 at 14:01











  • What prevents you to call g() after f() and not g() from f()?

    – vahancho
    Jun 18 '14 at 14:11











  • So, I want g() to be called from the next event stack of message loop which means that instead of calling g() at this time, I just want to post an event in message loop and call that later when current event processing is complete. I hope it clarifies the things.

    – Aarkan
    Jun 18 '14 at 14:32














2












2








2








I have a function in Qt 4.8 (windows), which has 3 things to do, like this:



void f()
{
//1. Do initialization

//2. Defer g() for next event (on the same thread) so that it may execute after f() is over

//3. Call h() which is time consuming
}


Now, I am not sure how to implement the 2nd step i.e. how to ensure that g() is called after the call to f() is complete, on the same thread (but on the different stack). I tried using QTimer but if I provide a short time say 10 ms, the function g() is called even when f() has not completed execution. So, relying on timers does not seem to be a reliable idea. Please help, any ideas are much appreciated.










share|improve this question














I have a function in Qt 4.8 (windows), which has 3 things to do, like this:



void f()
{
//1. Do initialization

//2. Defer g() for next event (on the same thread) so that it may execute after f() is over

//3. Call h() which is time consuming
}


Now, I am not sure how to implement the 2nd step i.e. how to ensure that g() is called after the call to f() is complete, on the same thread (but on the different stack). I tried using QTimer but if I provide a short time say 10 ms, the function g() is called even when f() has not completed execution. So, relying on timers does not seem to be a reliable idea. Please help, any ideas are much appreciated.







qt qt4






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jun 18 '14 at 13:58









AarkanAarkan

1,45342244




1,45342244













  • What does this mean on the same thread (but on the different stack)?

    – Alex F
    Jun 18 '14 at 14:01











  • What prevents you to call g() after f() and not g() from f()?

    – vahancho
    Jun 18 '14 at 14:11











  • So, I want g() to be called from the next event stack of message loop which means that instead of calling g() at this time, I just want to post an event in message loop and call that later when current event processing is complete. I hope it clarifies the things.

    – Aarkan
    Jun 18 '14 at 14:32



















  • What does this mean on the same thread (but on the different stack)?

    – Alex F
    Jun 18 '14 at 14:01











  • What prevents you to call g() after f() and not g() from f()?

    – vahancho
    Jun 18 '14 at 14:11











  • So, I want g() to be called from the next event stack of message loop which means that instead of calling g() at this time, I just want to post an event in message loop and call that later when current event processing is complete. I hope it clarifies the things.

    – Aarkan
    Jun 18 '14 at 14:32

















What does this mean on the same thread (but on the different stack)?

– Alex F
Jun 18 '14 at 14:01





What does this mean on the same thread (but on the different stack)?

– Alex F
Jun 18 '14 at 14:01













What prevents you to call g() after f() and not g() from f()?

– vahancho
Jun 18 '14 at 14:11





What prevents you to call g() after f() and not g() from f()?

– vahancho
Jun 18 '14 at 14:11













So, I want g() to be called from the next event stack of message loop which means that instead of calling g() at this time, I just want to post an event in message loop and call that later when current event processing is complete. I hope it clarifies the things.

– Aarkan
Jun 18 '14 at 14:32





So, I want g() to be called from the next event stack of message loop which means that instead of calling g() at this time, I just want to post an event in message loop and call that later when current event processing is complete. I hope it clarifies the things.

– Aarkan
Jun 18 '14 at 14:32












2 Answers
2






active

oldest

votes


















4














Make g() a slot or a Q_INVOKABLE. Then use QMetaObject::invokeMethod(this, "g", Qt::QueuedConnection).






share|improve this answer































    0














    If I understand correctly, it's needed simply to call g() after f(), but on the next message loop cycle, after all Qt events issued from f() would be processed. In this case I suggest the following trick:



    f();
    QApplication::processEvents();
    g();





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24287562%2fdefer-a-function-call-in-qt%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      4














      Make g() a slot or a Q_INVOKABLE. Then use QMetaObject::invokeMethod(this, "g", Qt::QueuedConnection).






      share|improve this answer




























        4














        Make g() a slot or a Q_INVOKABLE. Then use QMetaObject::invokeMethod(this, "g", Qt::QueuedConnection).






        share|improve this answer


























          4












          4








          4







          Make g() a slot or a Q_INVOKABLE. Then use QMetaObject::invokeMethod(this, "g", Qt::QueuedConnection).






          share|improve this answer













          Make g() a slot or a Q_INVOKABLE. Then use QMetaObject::invokeMethod(this, "g", Qt::QueuedConnection).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jun 18 '14 at 18:20









          Kuba OberKuba Ober

          69.7k982186




          69.7k982186

























              0














              If I understand correctly, it's needed simply to call g() after f(), but on the next message loop cycle, after all Qt events issued from f() would be processed. In this case I suggest the following trick:



              f();
              QApplication::processEvents();
              g();





              share|improve this answer




























                0














                If I understand correctly, it's needed simply to call g() after f(), but on the next message loop cycle, after all Qt events issued from f() would be processed. In this case I suggest the following trick:



                f();
                QApplication::processEvents();
                g();





                share|improve this answer


























                  0












                  0








                  0







                  If I understand correctly, it's needed simply to call g() after f(), but on the next message loop cycle, after all Qt events issued from f() would be processed. In this case I suggest the following trick:



                  f();
                  QApplication::processEvents();
                  g();





                  share|improve this answer













                  If I understand correctly, it's needed simply to call g() after f(), but on the next message loop cycle, after all Qt events issued from f() would be processed. In this case I suggest the following trick:



                  f();
                  QApplication::processEvents();
                  g();






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 20 '18 at 18:33









                  RedSoftRedSoft

                  111111




                  111111






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24287562%2fdefer-a-function-call-in-qt%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      'app-layout' is not a known element: how to share Component with different Modules

                      android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                      WPF add header to Image with URL pettitions [duplicate]