PDA

View Full Version : QTimer or recursive calls for simulation?



Morea
10th May 2006, 09:40
Hi.
I'm not sure which way to do this: I would like to call a function simulate() repeatedly and the function, after having done some calculations, calls update() and draws on the screen.
There seems to be two ways, either having a timer that timesout and then calls simulate(), or to have the function call it self after doing the update.

I would like to have a start/stop button for the simulation. With the timer, that is just to make a button click tell the timer to stop, but with the recursive call, I think I have then to set a bool variable that tells simulate() to stop.

Or perhaps a third way, not ,making it recursive, but instead emit a signal that connects to the simulate() slot?
The question is: if you understand what I want, which way is the better?

e8johan
10th May 2006, 11:08
The QTimer is the way to go. Calling recursively will fill the stack crashing your application, as will the signals/slot approach (at least in Qt3).

Morea
10th May 2006, 11:16
Great!
I would really like it to draw at maximum speed without any pauses (even if they are really short) and I wish to avoid any problem if it timeout before drawing and calculations are done.
Is there a non-crashing solution to this other than to generate all data and then animate it afterwards?

wysota
12th May 2006, 00:19
Use a singleshot QTimer with 0 timeout time. It will trigger instantly when all previous events are handled.


void MyClass::myTimeoutSlot(){
doSomething();
update();
QTimer::singleShot(0, this, SLOT(myTimeoutSlot()));
}