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. QTimer not accurate?
QtWS25 Last Chance

QTimer not accurate?

Scheduled Pinned Locked Moved General and Desktop
15 Posts 4 Posters 12.8k 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.
  • JKSHJ Offline
    JKSHJ Offline
    JKSH
    Moderators
    wrote on last edited by
    #6

    You're welcome :)

    [quote author="maximus" date="1380195467"]
    For showing the time, the method I use is probably not right.
    I initialize a "QTime(0,0,0,0)" and every 1sec my QTimer increase QTime by 1sec[/quote]That method causes errors to keep growing over time. The QTimer isn't going to fire exactly every 1000 ms, especially if you're using a coarse timer.

    If every time your QTimer fires it's off by 15 ms, then after 5 minutes (300 seconds) you could have an accumulated error of 300*15 ms = 4500 ms

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

    1 Reply Last reply
    0
    • M Offline
      M Offline
      maximus
      wrote on last edited by
      #7

      [quote author="JKSH" date="1380196428"]You're welcome :)
      That method causes errors to keep growing over time. The QTimer isn't going to fire exactly every 1000 ms, especially if you're using a coarse timer.

      If every time your QTimer fires it's off by 15 ms, then after 5 minutes (300 seconds) you could have an accumulated error of 300*15 ms = 4500 ms[/quote]

      Yes you were right, I tried with "Qt::PreciseTimer" and it stop right on 5min, so that must be the problem (this machine is on Windows)
      However I can't use this method as I want this application multiplatform and free. I'll see if QElapsedTimer can do the same work


      Free Indoor Cycling Software - https://maximumtrainer.com

      1 Reply Last reply
      0
      • M Offline
        M Offline
        maximus
        wrote on last edited by
        #8

        It seem QElapsedTimer will not be suitable in my use-case, because the timer sometime needs to be stopped (pause the workout)
        So I guess I'll have to use QTimer.
        Is there a reliable timer for Mac/Linux, like Qt::PreciseTimer is for Windows?
        I could check the OS and set the timerType accordingly.

        Anyone know if I would have this problem with Java and Timer? I'm not 100% sure i'll use QT over Java.
        Thank you.


        Free Indoor Cycling Software - https://maximumtrainer.com

        1 Reply Last reply
        0
        • JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #9

          [quote author="maximus" date="1380203827"]It seem QElapsedTimer will not be suitable in my use-case, because the timer sometime needs to be stopped (pause the workout)[/quote]You can record the elapsed time when the user stops the timer. Then, when the user starts the timer again, add the previous record to to the new timer's values.

          [quote]Is there a reliable timer for Mac/Linux, like Qt::PreciseTimer is for Windows?
          I could check the OS and set the timerType accordingly.[/quote]Qt is cross-platform. Qt::PreciseTimer exists on all supported platforms, including Mac and Linux.

          I only mentioned Windows because I tested the timers on Windows. But, it's often said Qt;:CoarseTimer is more accurate on Linux than on Windows.

          [quote]Anyone know if I would have this problem with Java and Timer? I'm not 100% sure i'll use QT over Java. [/quote]I don't use Java, so I don't know the answer to that. But, like Qt, it probably depends on the OS and hardware.

          What are your plans for you application?

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

          1 Reply Last reply
          0
          • M Offline
            M Offline
            maximus
            wrote on last edited by
            #10

            [quote author="JKSH" date="1380205541"]You can record the elapsed time when the user stops the timer. Then, when the user starts the timer again, add the previous record to to the new timer's values.[/quote]

            Good idea!

            [quote]I don't use Java, so I don't know the answer to that. But, like Qt, it probably depends on the OS and hardware.[/quote]

            I was asking because I think Timer in Java will use the appropriate timer depending on your OS.

            [quote]What are your plans for you application?[/quote]

            Why I chose QT: Ease of developing interface, cross-platform and faster execution than Java, Con: i'm kind of new to C++
            As for Java I just have more experience, and the deployment is easier

            I want to develop something similar to :
            http://www.trainerroad.com/
            but cross-platform and no fee

            Thanks for your time :)


            Free Indoor Cycling Software - https://maximumtrainer.com

            1 Reply Last reply
            0
            • M Offline
              M Offline
              maximus
              wrote on last edited by
              #11

              Just an update,
              since the timer was not precise enough in Windows only,
              I added the specific code and it fix the problem :

              #ifdef WIN32
              timer_1sec->setTimerType(Qt::PreciseTimer);
              #endif

              On other platform the standard timer type seem accurate enough for my need.
              Thanks everyone


              Free Indoor Cycling Software - https://maximumtrainer.com

              1 Reply Last reply
              0
              • JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by
                #12

                [quote]I was asking because I think Timer in Java will use the appropriate timer depending on your OS.[/quote]What's "appropriate" depends on your use case too -- the programming framework doesn't know your plans so it can't make decisions for you.

                High-precision timers have more overhead. You probably won't notice the overhead in you app, but generally it's good practice to use the cheapest option available that meets your requirements. No point using something expensive that you don't need.

                [quote]since the timer was not precise enough in Windows only,
                I added the specific code and it fix the problem :
                #ifdef WIN32 timer_1sec->setTimerType(Qt::PreciseTimer);
                #endif
                On other platform the standard timer type seem accurate enough for my need.[/quote]Another way to reduce risk of your app stopping at "4:99" or "5:01": Instead of using a separate timer to signal the end of the workout, just add another check after you measure your elapsed time: If elapsed time >= 300000 (or if your clock says "5:00"), then stop the session.

                [quote]Why I chose QT: Ease of developing interface, cross-platform and faster execution than Java, Con: i'm kind of new to C++
                As for Java I just have more experience, and the deployment is easier

                I want to develop something similar to :
                http://www.trainerroad.com/
                but cross-platform and no fee
                [/quote]A very good cause. All the best!

                If you plan to make more apps in the future (and especially if you plan to target different mobile platforms), it's definitely worth investing time in Qt :) Here's an overview of all features in Qt, which is a lot more than GUI development: http://doc-snapshot.qt-project.org/qt5-stable/qtmodules.html

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

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  maximus
                  wrote on last edited by
                  #13

                  [quote]Another way to reduce risk of your app stopping at "4:99" or "5:01": Instead of using a separate timer to signal the end of the workout, just add another check after you measure your elapsed time: If elapsed time >= 300000 (or if your clock says "5:00"), then stop the session.
                  [/quote]

                  Yes now I only use one timer and assume it's right, all actions come from it, safer this way.

                  [quote]
                  A very good cause. All the best!

                  If you plan to make more apps in the future (and especially if you plan to target different mobile platforms), it's definitely worth investing time in Qt :) Here's an overview of all features in Qt, which is a lot more than GUI development: http://doc-snapshot.qt-project.org/qt5-stable/qtmodules.html[/quote]

                  Thanks for you support! the community seems really nice and great support with tutorials, etc.
                  I guess the hard part for me will be to compile a version of my app for all platform. I would like to support Mac/Linux/Windows, for android and IOs, is it possible also? I use the latest qt5.1 with a qwt plugin. right now I just use qtcreator in ubuntu and it compiles and works fine. Is it possible to do a setup so I can build/compile/deploy all OS version on a single machine? I'll go read more on this sorry for the newbie questions.

                  Have a great night,
                  Max


                  Free Indoor Cycling Software - https://maximumtrainer.com

                  1 Reply Last reply
                  0
                  • JKSHJ Offline
                    JKSHJ Offline
                    JKSH
                    Moderators
                    wrote on last edited by
                    #14

                    [quote author="maximus" date="1380241996"]
                    Thanks for you support! the community seems really nice and great support with tutorials, etc.[/quote]You're most welcome. :) Yes, I've learnt lots from this community too. If you have any more questions, just start a new post.

                    [quote]I guess the hard part for me will be to compile a version of my app for all platform. I would like to support Mac/Linux/Windows, for android and IOs, is it possible also? I use the latest qt5.1 with a qwt plugin. right now I just use qtcreator in ubuntu and it compiles and works fine. Is it possible to do a setup so I can build/compile/deploy all OS version on a single machine? I'll go read more on this sorry for the newbie questions.[/quote]Using one platform to build programs for a different platform is called "cross-compiling", and Qt Creator has some built-in support for that.

                    Android and iOS support is in "Technology Preview" mode at the moment. Full support will come in November when Qt 5.2 is released.

                    On your development PC, you'll need compiled Qt libraries for each platform that you're targetting.

                    MinGW is a very solid toolchain for producing Windows executables; you can use this to create a Windows .exe from Ubuntu. It seems harder to target Mac though; I found this but it doesn't sound very robust: http://stackoverflow.com/questions/2786240/how-to-compile-intel-mac-binaries-on-linux

                    But anyway, this is a different topic now. It's best to start a new post if you have more questions about cross-compiling.

                    Again, good luck.

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

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      maximus
                      wrote on last edited by
                      #15

                      Just to keep you guys updated, I found a good solution with QwtSystemClock (use QElapsedTimer internally).

                      I used the method similar to the example oscilloscope in qwt/example
                      Variable used:

                      @ int d_timerId;
                      QwtSystemClock d_clock;
                      int lastSecond;
                      double timeElapsed_sec;
                      double timePaused_sec;
                      double resumeActivated_time_sec;
                      double pauseActivated_time_sec;@

                      code :

                      @void WorkoutDialog::startWorkout() {

                      d_clock.start();
                      d_timerId = startTimer( 1 );  //check ElapsedTime every 1ms! Is there a better way to monitor Elapsed time? can Elapsedtime trigger an event every 1sec without the need to monitor it with a non-reliable timer??
                      

                      }

                      ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                      /// timerEvent
                      ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                      void WorkoutDialog::timerEvent( QTimerEvent *event ) {

                      if ( event->timerId() == d_timerId && !isWorkoutPaused ) {
                      
                          timeElapsed_sec = d_clock.elapsed() / 1000.0 - timePaused_sec;
                      

                      // qDebug() << "time Elapsed:" << d_clock.elapsed() / 1000.0;
                      // qDebug() << "Total time paused:" << timePaused_sec;
                      // qDebug() << "Resulting true Elapsed time" << timeElapsed_sec;
                      // qDebug() << "----------------------------";

                          int currentSecond = ((int)timeElapsed_sec) % 10;
                          //        qDebug() << "currentSecond : " << currentSecond;
                      
                          // 1 sec has passed
                          if (currentSecond != lastSecond) {
                              update1sec();
                          }
                          lastSecond = currentSecond;
                      }
                      

                      }@

                      I just did a test and on a 4hours workout, I lost 2 seconds in precision, pretty good.
                      The lost come from :
                      int currentSecond = ((int)timeElapsed_sec) % 10;
                      that removes over time a fraction on a second
                      Maybe I could improve it even more by calculating an acceptable interval instead of casting?
                      Like :
                      if (timeElapsed_sec - ((int) timeElapsed_sec) < 0,001)
                      so close it counts as a second...

                      Let me know if you have ideas
                      Thanks!


                      Free Indoor Cycling Software - https://maximumtrainer.com

                      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