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. Which is the correct way to create a pause/waiting in QT?
Forum Updated to NodeBB v4.3 + New Features

Which is the correct way to create a pause/waiting in QT?

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 5 Posters 5.1k Views 1 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.
  • E Offline
    E Offline
    EricLiii
    wrote on last edited by
    #1

    Hi all,

    I want to create a pause/waiting to pause current thread while still allow other threads to process their events. But I am not sure which way below is correct.

    QTime t;
    t.start();
    while (t.elapsed() < 1000)
    {
    QCoreApplication::processEvents();
    }

    OR

    QCoreApplication::processEvents();
    QThread::msleep(1000);

    So anyone can help me?
    Please let me know if you have better solution.

    Thanks!

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      When it's a separate thread and not the gui thread then QThread::msleep(1000); is ok - otherwise I simply would use a singleshot QTimer.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

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

        Hi,

        Neither is a good idea as you are not waiting for your other threads to finish you are just "sleeping".

        What exactly are you waiting on ?
        Depending on that, you might want to consider modeling your system with a state machine.

        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
        2
        • Christian EhrlicherC Christian Ehrlicher

          When it's a separate thread and not the gui thread then QThread::msleep(1000); is ok - otherwise I simply would use a singleshot QTimer.

          E Offline
          E Offline
          EricLiii
          wrote on last edited by
          #4

          @christian-ehrlicher
          Got it, thanks!

          1 Reply Last reply
          0
          • E Offline
            E Offline
            EricLiii
            wrote on last edited by
            #5

            @sgaist
            Yeah, you're right. Actually I want to wait for other threads to finish first, then continue to do something in current thread. But I have no idea about how to know the other threads have finished or not, so I just set a sleeping time by experience.

            aha_1980A 1 Reply Last reply
            0
            • E EricLiii

              @sgaist
              Yeah, you're right. Actually I want to wait for other threads to finish first, then continue to do something in current thread. But I have no idea about how to know the other threads have finished or not, so I just set a sleeping time by experience.

              aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @ericliii Emit a signal from the other threads when they are finished. Connect that signal to a slot in the main thread, and handle the finished state there.

              Regards

              Qt has to stay free or it will die.

              E 1 Reply Last reply
              6
              • aha_1980A aha_1980

                @ericliii Emit a signal from the other threads when they are finished. Connect that signal to a slot in the main thread, and handle the finished state there.

                Regards

                E Offline
                E Offline
                EricLiii
                wrote on last edited by
                #7

                @aha_1980
                Sounds good, thanks!

                JonBJ 1 Reply Last reply
                0
                • E EricLiii

                  @aha_1980
                  Sounds good, thanks!

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @ericliii
                  Although @aha_1980 tells you to "Emit a signal from the other threads when they are finished", I would have thought that the existing https://doc.qt.io/qt-5/qthread.html#finished signal does just that for you? Having said that, have a read of https://stackoverflow.com/questions/17045368/qthread-emits-finished-signal-but-isrunning-returns-true-and-isfinished-re accepted solution, and https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

                  E 1 Reply Last reply
                  2
                  • JonBJ JonB

                    @ericliii
                    Although @aha_1980 tells you to "Emit a signal from the other threads when they are finished", I would have thought that the existing https://doc.qt.io/qt-5/qthread.html#finished signal does just that for you? Having said that, have a read of https://stackoverflow.com/questions/17045368/qthread-emits-finished-signal-but-isrunning-returns-true-and-isfinished-re accepted solution, and https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

                    E Offline
                    E Offline
                    EricLiii
                    wrote on last edited by
                    #9

                    @jonb
                    Thanks for you answer!

                    void QThread::finished() is useful but not what I exactly want. I just need to wait for a specific event in another thread to finish. No need to sleep until the whole thread is finished. So I think signal-slot is a better choice.

                    JonBJ 1 Reply Last reply
                    1
                    • E EricLiii

                      @jonb
                      Thanks for you answer!

                      void QThread::finished() is useful but not what I exactly want. I just need to wait for a specific event in another thread to finish. No need to sleep until the whole thread is finished. So I think signal-slot is a better choice.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #10

                      @ericliii
                      void QThread::finished() is already just as much "signal-slot" as your own explicit one, just saying! But yes emit your own signal if you want to know earlier than thread finish.

                      E 1 Reply Last reply
                      1
                      • JonBJ JonB

                        @ericliii
                        void QThread::finished() is already just as much "signal-slot" as your own explicit one, just saying! But yes emit your own signal if you want to know earlier than thread finish.

                        E Offline
                        E Offline
                        EricLiii
                        wrote on last edited by
                        #11

                        @jonb
                        Okay, I got it. Thanks bro!

                        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 Extensions
                        • Unsolved