Start() Function in QTimer not functioning Properly



  • 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....



  • Can you please share some code?



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

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



  • 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 ???



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



  • 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)



  • 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???


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.