Monday, 15 November 2010

In praise of QTimer->start(0)

The situation: In Qt I have to get some data ready in an application but it doesn't all need to be ready at the start so I want to get some to begin with and then continue getting the rest in the background without freezing the UI. Also I need to be able to stop this data getting for when I want to run a high precision timer with high accuracy.

First attempted solution: Subclassing QThread (though this is not recommended, see
Basically in this case, i make the run method of a QThread subclass do a little piece of the data processing. There are 2 disadvantages to this method, firstly, it has a large overhead creating a whole thread and secondly you also have to implement your own pause and resume functions as QThread does not come with these.

The used solution: Write a slot that performs a single part of the data processing, connect it to the timeout() slot of a QTimer and then call QTimer->start(0) where the 0 is a special value to QTimer indicating that it should only fire when the application is idle (see This method also makes pausing and resuming very very simple as to pause you just need to call QTimer->stop() and then to start again call QTimer->start(0).

This solution works ridiculously well in my application and the ease of pausing and resuming really is a treat.

No comments:

Post a Comment