PDA

View Full Version : Problem wit QTimer, different time, why?



perseo
6th August 2008, 11:04
Hello!!
I am implementing one video reproducer and the refresh is 40 ms.
To make this i use one timer:


timer = new QTimer();
connect(timer, SIGNAL(timeout()), this, SLOT(updateLabel()));
timer->start(40);

updateLabel()
{
XXXXXXXXXX
Label->setPixmap(QPixmap::fromImage(qimg_l));
}

But when i load the video the first time is slower than the second, third, ...
I use for example one refresh of 1s and the video is loaded too the first time at 1s for each frame, but the second, third, faster than the first.
Why this thing? Any idea?

Thank you

perseo
7th August 2008, 10:46
No idea? Can you help me?
Thanks!

wysota
7th August 2008, 13:10
The timer will not be fired every 40ms, but not earlier than 40ms after the last timeout.

luf
7th August 2008, 13:51
The timer will not be fired every 40ms, but not earlier than 40ms after the last timeout.

Even though the QTimer docs says they are 1 ms accurate, that's partially true, because QTimers are depending on other tasks in your application. For fun you can add a few 1000 qtimers, and plot the time for each of them to see what wysota is talking about. (Or find something that maxes your cpu usage while using qtimers in another program...)

You can use QDateTime and get the milliseconds timer to do a better timing. for instance if you need 40ms timing, set the timer to something faster than 40 ms and use the milliseconds from qdatetime to trigger en event at exactly 40ms from last time. (just save your last-time somewhere...) Although this is not a particullary good solution..

I suggest reading this article covering the subject with some data and tests:
The Qt Framework and (soft) real-time projects (http://www.folding-hyperspace.com/program_tip_14.htm)

Short summary is in win32, use win32 native periodic timer, on linux, use RTC wait.

you also might want to run your timing in a seperate thread/process...

cheers,
leif

wysota
7th August 2008, 21:23
Even though the QTimer docs says they are 1 ms accurate, that's partially true,
They are not 1ms accurate, they have a resolution of 1ms and that's two different things.

luf
8th August 2008, 08:41
They are not 1ms accurate, they have a resolution of 1ms and that's two different things.

Thanks for correcting! Of course it's resolution... as if they were 1 ms accurate, the problem wouldn't be there!

On the other hand, I read from the docs that the "accuracy depends on the underlying platform", and "Most platforms support an accuracy of 1 millisecond". That was made me think accuracy, while it's really resolution it should say.

thanks,
leif

wysota
8th August 2008, 09:41
You can report a bug in the docs then ;) It should definitely say "resolution" there.