Results 1 to 4 of 4

Thread: QTimer or recursive calls for simulation?

  1. #1
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTimer or recursive calls for simulation?

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Alingsås, Sweden
    Posts
    437
    Thanks
    3
    Thanked 39 Times in 39 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTimer or recursive calls for simulation?

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

  3. #3
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTimer or recursive calls for simulation?

    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?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTimer or recursive calls for simulation?

    Use a singleshot QTimer with 0 timeout time. It will trigger instantly when all previous events are handled.

    Qt Code:
    1. void MyClass::myTimeoutSlot(){
    2. doSomething();
    3. update();
    4. QTimer::singleShot(0, this, SLOT(myTimeoutSlot()));
    5. }
    To copy to clipboard, switch view to plain text mode 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.