0
votes

I'm building a simple game that shows a countdown to the user, starting at 1:00 and counting down to zero. As 0:00 is reached, I want to display a message like "time's up!".

I currently have a QTimer and a QTime object (QTime starting at 00:01:00)

QTimer *timer=new QTimer();
QTime time{0,1,0};

In the constructor I'm setting the timer to timeout every 1 second, and it's connected to an event that updates the countdown on screen, which is initially displaying the timer at 1:00:

connect(timer, SIGNAL(timeout()), this, SLOT(updateCountDown()));
timer->start(1000);
ui->countdown->setText(time.toString("m:ss"));

This is the slot being called every 1 second:

void MainWindow::updateCountDown(){
    time=time.addSecs(-1);
    ui->countDown->setText(time.toString("m:ss"));
}

Now I need to be able to call a new method whenever the QTime reaches 0:00. I'm not very keen on adding an if on the updateCountdown method to check if the QTime is at 0:00 every second. I also thought maybe I could add a second QTimer that times out at 1 minute, but I'm not sure if both QTimer objects will start at the exact same time so the 1 minute timeout will happen exactly when the QTime object is at 0:00.

So is there a way to add a second timeout to the same QTimer object (to timeout once every second to update the countdown on screen and then a second timeout after 1 minute to end the game? I suspect the answer will be "no", but in that case, what would be the best approach? (if none of my options are valid, is there a better way to do it?).

1
Why don't you just use two different timers? - Jesper Juhl
From what I understand you want to show the time every second and if the time runs out, that is, reaches 0:00 then call another function. I am right? - eyllanesc
@JesperJuhl using 2 timers is one of the approaches I thought of, but I'm not sure if they will start at the same time (maybe they're on different threads or some other implementation specific stuff makes them start differently on each program run). - Floella
"I'm not very keen on adding an if on the updateCountdown method to check if the QTime is at 0:00 every second." Why not? That's what you should be doing. - Nikos C.
@Floella As indicated by Nikos C, the correct approach is to verify. - eyllanesc

1 Answers

3
votes

The answer to your first question is no -- QTimer supports a single timeout (or a single specified time-interval, if it's not running in single-shot mode).

The answer to your second question is -- the best approach is the simplest one that can possibly work. Use a single QTimer, and in your updateCountdown() method, include an if statement to do something different when the countdown finally reaches zero. (Btw you don't really need to use a QTime object to represent the countdown; you could just as easily use an int that starts at 60 and is decremented until it reaches 0)

Why is this better/simpler? You could use two QTimer objects instead, but then you have to worry about keeping them in sync -- perhaps that's not a big deal for now, but what happens when you want to add a "Pause" button to your game, or when you want to add a time-bonus that gives the player 10 extra seconds of play time? All of a sudden, your 60-second timer will no longer be doing the right thing, and it will be a pain to stop and restart it correctly in all cases.

If-statements are cheap, and easy to understand/control/debug; so you might as well use one.