c++qtslowdown

How to slow down a method execution without freezing the thread/application?


Everything is in the title :)

Why I need this :

I've created a small playable sudoku game in C++ with Qt. I've made a classic backtracking method to resolve it. The user can see the resolution progress in real-time. I've put a slider allowing the user to adjust the resolution speed.

That's what I've put at the begging of the resolution method to slow down the process :

 sleep_for(milliseconds(delay)); //delay is a toggleable static variable

BUT since the user launch the resolution, he can't change the speed(delay) because the application is sleeping most of the time.

Any other way to slow down the process ?

ps : please excuse my rough english :/


Solution

  • Use timerEvent(). QObject has timer support built in. Put each game step in the timerEvent() and call startTimer() with the required interval. This is by far the easiest way to do it.