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. [Solved] How to set a delay ?
QtWS25 Last Chance

[Solved] How to set a delay ?

Scheduled Pinned Locked Moved General and Desktop
15 Posts 12 Posters 129.7k Views
  • 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.
  • G Offline
    G Offline
    guziemic
    wrote on 11 Oct 2012, 10:40 last edited by
    #3

    One of solution which comes to my mind is local event loop. But it is probably not the best one.
    There is some example of usage "Example":http://qt-project.org/forums/viewthread/19023/
    On this forum I saw some posts about drawback and advantages of such solutions.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on 11 Oct 2012, 10:41 last edited by
      #4

      [quote author="Sam" date="1349950497"]the calculations are done in the paintEvent()[/quote]
      Don't do that. Implementations of paintEvent need to be as fast as possible to keep your application snappy.

      Instead, you should decouple your display of progress with the actual process you're monitoring. Note that implementing a delay is almost never the (right) solution to any programming problem[1]. Instead, just make your application fast. Your users will thank you for it.

      [1] an exception might be software that needs to communicate with hardware where such delays are sometimes unavoidable.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        KA51O
        wrote on 11 Oct 2012, 11:30 last edited by
        #5

        instead of a loop you could have a function like this:

        @
        int m_counterForDelayedLoop = 0; //this is a member var
        void loopFunctionWithDelay()
        {
        doSomething();
        if(m_counterForDelayedLoop < 100)
        {
        m_counterForDelayedLoop++;
        QTimer::singleShot(500, this, loopFunctionWithDelay());
        }
        }
        @

        But I think you better follow the suggestions from MuldeR and Andre.

        1 Reply Last reply
        0
        • J Offline
          J Offline
          Jeroentjehome
          wrote on 11 Oct 2012, 12:12 last edited by
          #6

          If it is only to update the progressbar every so often, why not create a slot to update the progressbar. create a signal that is emitted when you have a doSomething update (e.g. 1% increased).
          This way the GUI is only called 100 times in the entire loop and the operation is as fast as possible. As Andre says it is a very bad programming practice to insert delays or sleep commands. Your on an event driven machine. Not a time / sequential embedded "C" system.
          Greetz

          Greetz, Jeroen

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Sam
            wrote on 11 Oct 2012, 14:04 last edited by
            #7

            Thanks to everyone,

            As suggested by Andre I changed the approach and the calculations are all removed from the paintEvent() and calculated in a seperate function , So in the paintEvent it just draws/fill the pixmap. The requirement of a delay is just for the test , filling of the pixmap will be connected to the process that will be monitored, but that comes from the server.

            The example provided by guziemic did the job

            Thanks for the help

            1 Reply Last reply
            0
            • B Offline
              B Offline
              BasicPoke
              wrote on 16 Jul 2014, 22:19 last edited by
              #8

              Any problem using this function for general delays? I haven't tried it.

              @void QTest::qWait(int ms) [static]@

              Ron

              1 Reply Last reply
              1
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 17 Jul 2014, 07:10 last edited by
                #9

                Yes, like the framework name suggests, it's designed for unit testing, not production code.

                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
                0
                • B Offline
                  B Offline
                  BasicPoke
                  wrote on 17 Jul 2014, 13:40 last edited by
                  #10

                  QTest is not compiling for me. I'm using Qt 5.2.1 MinGW 32 bit. When I added @#include <QTest>@ I get "QTest: No such file or directory."

                  Then I tried adding:
                  INCLUDEPATH += C:/Qt/Qt5.2.1/5.2.1/mingw48_32/include/QtTest
                  to my pro file, then I get "expected primary-expression before 'atest'." Any help please?

                  @QTest atest; atest.qWait(10);@

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    JKSH
                    Moderators
                    wrote on 17 Jul 2014, 13:43 last edited by
                    #11

                    Like SGaist said, you should not use QTest for general delays. QTest functions are for unit testing.

                    You can use QThread::sleep() or QThread::msleep().

                    Remember that waiting/sleeping will freeze your thread, which might reduce your program's performance.

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

                    A 1 Reply Last reply 12 Mar 2019, 13:18
                    0
                    • B Offline
                      B Offline
                      BasicPoke
                      wrote on 17 Jul 2014, 14:27 last edited by
                      #12

                      I got the following to work. Thanks for the help.

                      @#include <QThread>
                      QThread::msleep(100);@

                      1 Reply Last reply
                      0
                      • B Offline
                        B Offline
                        Bittoo
                        wrote on 14 Aug 2014, 07:04 last edited by
                        #13

                        Dear BasicPoke,
                        ​you can also use QThread::usleep(5) in a loop till you back-end/other process is not finished. You can use signals\slot to connect. Once your back-end is done you can send signal to handleDBOperatinOnWaitDone(bool ) and do the rest of work. This works fine with multi-threaded application too.

                        @
                        class ResolutionCalculationBS : public QThread{
                        .
                        .
                        .
                        private:
                        volatile bool dbOperatinOn;
                        .
                        .
                        .
                        };

                        void ResolutionCalculationBS::dbOperatinOnWait()
                        {
                        this->dbOperatinOn = true;
                        while (this->dbOperatinOn) {
                        QThread::usleep(5);
                        }//end while
                        return;
                        }//end dbOperatinOnWait

                        void ResolutionCalculationBS::handleDBOperatinOnWaitDone(bool flag)
                        {
                        QMutex mutex;
                        QMutexLocker locker(&mutex);
                        this->dbOperatinOn = flag;
                        }
                        @

                        Regards,
                        Manoj Kumar Panwar,

                        1 Reply Last reply
                        0
                        • J JKSH
                          17 Jul 2014, 13:43

                          Like SGaist said, you should not use QTest for general delays. QTest functions are for unit testing.

                          You can use QThread::sleep() or QThread::msleep().

                          Remember that waiting/sleeping will freeze your thread, which might reduce your program's performance.

                          A Offline
                          A Offline
                          Anna_64
                          wrote on 12 Mar 2019, 13:18 last edited by
                          #14

                          @JKSH then how to introduce a wait because whenever i ma using sleep my program freezes

                          aha_1980A 1 Reply Last reply 12 Mar 2019, 13:45
                          0
                          • A Anna_64
                            12 Mar 2019, 13:18

                            @JKSH then how to introduce a wait because whenever i ma using sleep my program freezes

                            aha_1980A Offline
                            aha_1980A Offline
                            aha_1980
                            Lifetime Qt Champion
                            wrote on 12 Mar 2019, 13:45 last edited by
                            #15

                            @Anna_64

                            You can use QTimer::singleShot() to execute a certain piece of code after a wait time.

                            Whenever you use some kind of sleep, you GUI program freezes, that has been the case since GUIs were invented.

                            Qt has to stay free or it will die.

                            1 Reply Last reply
                            3

                            • Login

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