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. Start() Function in QTimer not functioning Properly
Forum Updated to NodeBB v4.3 + New Features

Start() Function in QTimer not functioning Properly

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 2.7k 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.
  • R Offline
    R Offline
    rajatgupta431
    wrote on last edited by
    #1

    I am trying to make a Stopwatch sort of thing using the Qtimer class whose time out function is emitted after every
    100 msec using start(100). But its running too fast than 100 msec....the speed goes on increasing....

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MuldeR
      wrote on last edited by
      #2

      Can you please share some code?

      My OpenSource software at: http://muldersoft.com/

      Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

      Go visit the coop: http://youtu.be/Jay...

      1 Reply Last reply
      0
      • R Offline
        R Offline
        rajatgupta431
        wrote on last edited by
        #3

        @void stopwatch::forsignal()
        {
        connect(time,SIGNAL(timeout()),this,SLOT(updateTime()));
        time->start(100);
        }

        void stopwatch::updateTime()
        {
        lcd->display(i);
        i=i+.1;
        }@

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MuldeR
          wrote on last edited by
          #4

          Your code looks good to me, so far.

          But: Are you 100% sure forsignal() is called exactly once? Also where and how is "i" declared? Are you sure each instance of "stopwatch" has its own "i" variable that is never used by another instance?

          Finally, how do you test that it's not working as expected ???

          My OpenSource software at: http://muldersoft.com/

          Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

          Go visit the coop: http://youtu.be/Jay...

          1 Reply Last reply
          0
          • R Offline
            R Offline
            rajatgupta431
            wrote on last edited by
            #5

            "i" is a static global variable of type float and yes the forsignal() is called only when the "Start" button is pressed....

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MuldeR
              wrote on last edited by
              #6

              I think "i" really shouldn't be global or static!

              You should make it a non-static member variable of the "stopwatch" class and initialize it to zero in the constructor. This way you make sure different "stopwatch" instances won't conflict! It will also make your code thread-safe, as long as each thread uses its own instance of your "stopwatch" class.

              --

              And why is it of type float and not (unsigned) integer? Floating point values have varying precision (large numbers have worse precision than small numbers). Also there are pitfalls when comparing floating point values. As a consequence, if, for example, you start with 0.0 and add 0.1 ten times, the result will not be (exactly) 1.0, but some approximation, because 0.1 cannot be represented accurately as floating point value.

              @float i = 0.0, j = 0.1;
              for(int q = 0; q < 10; q++)
              {
              i += j;
              }
              printf("%.16f\n", i);@
              Output is: 1.0000000149011612

              --

              Also be prepared for the case the user presses the button multiple times, e.g. ignore second press or reset.

              --

              Furthermore, you could add this code for test, so you will see what the actual delay is:
              http://pastie.org/private/cv6vqc3uc9xyxnjwjoe34q

              (Had to post the code on Pastie, because this forum is horribly broken and scrambles the code)

              My OpenSource software at: http://muldersoft.com/

              Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

              Go visit the coop: http://youtu.be/Jay...

              1 Reply Last reply
              0
              • R Offline
                R Offline
                rajatgupta431
                wrote on last edited by
                #7

                Thanks for the great help.....but i followed a bit different approach..it actually the forsignal() slot is being called whenever the START button is pressed...so the timeout signal was being emitted time and again...i fixed that and its working perfectly.....
                Can Someone help me how to make my Window Splash using QSplashscreen???

                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