[Qt-interest] Realtime timing in Qt

Thiago Macieira thiago at kde.org
Wed Sep 8 14:41:25 CEST 2010


Em Quarta-feira 08 Setembro 2010, às 13:51:29, Daniel Price escreveu:
> What's the simplest way to implement 'realtime' timing in Qt? I'm using a
> QGLWidget and I want to do some animation such that the framerate remains
> constants regardless of the size of the view or the complexity of the
> scene.
> 
> I've done this kind of thing before in Win32 using the Windows precision
> timers but I'm looking for a portable solution that makes use only of Qt.
> 
> I don't think QTimer will cut it because it is event-based. Ditto for
> QThreads and signal/slots.
> 
> Ideally I'd have a loop that checks the number of milliseconds passed since
> the last iteration. If the number is less than the required framerate, the
> (main) thread will sleep for the remainder, giving time to any other
> threads (sleep(0) in Win32).
> 
> How can I do this in Qt?

You're describing what QTimer already does.

If your handler doesn't overrun the time slot, the thread will go back to 
sleep for the remainder of the time.

If the handler does overrun, then the timer is reset and will sleep for a full 
new duration.

Note that QTimer uses whatever precision is coming from the system. QTimer and 
the Qt event loops have a granularity of 1 millisecond, but the underlying 
system may not honour that. On Unix, we're pretty sure of getting what we 
asked for. On Windows, I think Qt switches to HP timers if the timeout is less 
than 10 milliseconds. On Symbian I have no clue.

But that's still timeshared with other processes. If you wish to ensure proper 
realtime functionality, you must make the current thread have real-time 
priority. You need to do that on your own (usually require privileges).

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Senior Product Manager - Nokia, Qt Development Frameworks
      PGP/GPG: 0x6EF45358; fingerprint:
      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100908/f2de9912/attachment.bin 


More information about the Qt-interest-old mailing list