Reputation: 801
I update a count down timer using timerEvent(QTimerEvent *e)
once I am done I call the killTimer(timerID)
but the timerEvent()
is still being called.
So what's the proper way to kill it?
The Code:
void MainWindow::timerEvent(QTimerEvent *e)
{
Q_UNUSED(e);
static uint16_t u16RemTime = MAX_WARMUP_TIME_IN_SECS;
if((true == isWarmUpStarted) && (u16RemTime > 0))
{
u16RemTime--;
objptrSplashScreen->SetTime(u16RemTime);
}
else
{
//Still running
qWarning("\n\n\n\n\n WARM UP TIMER RUNNING \n\n\n\n\n");
killTimer(warmUpTimerID);
}
}
If it helps. I have two such timers running in two different classes in the same GUI thread. How would I go about killing it?
Upvotes: 1
Views: 4392
Reputation: 98425
You need to make sure that a particular timerEvent
invocation is related to your timer.
QBasicTimer
is a nice convenience wrapper around a timer id, you could use it instead of the raw id.
Static variables in members of classes that can be potentially reused are a source of nasty bugs.
isWarmupStarted
is redundant, its value is identical to m_warmupRemaining > 0
.
You're not saving anything by explicitly using a 16 bit unsigned integer for the remaining time counter. Just use an int.
The style that explicitly mentions types in variable names is, well, if your employer isn't forcing you to use it, don't use it. It's the compiler's job to keep track of such things, not yours, and it's not C and winapi where things sometimes got hairy if you didn't do that.
Thus:
class MainWindow : public QMainWindow {
Q_OBJECT
QSplashScreen * m_splashScreen;
QBasicTimer m_warmupTimer;
int m_warmupRemaining;
void timerEvent(QTimerEvent * ev) {
if (ev->timerId() != m_warmupTimer.timerId()) return;
// No need to call the empty QMainWindow::timerEvent(ev).
// All timerEvent implementations in public Qt classes are empty,
// to make your life easier.
if (m_warmupRemaining > 0) {
m_warmupRemaining --;
m_splashScreen->SetTime(m_warmupRemaining);
} else {
m_warmupTimer.stop();
}
}
};
Upvotes: 0
Reputation: 4286
timerEvent
receives all timers' events. In order to differ them QTimerEvent
class have int timerId() const
method. So your event should look like this:
void MainWindow::timerEvent(QTimerEvent *e)
{
if (e->timerId() != warmUpTimerID)
return;
static uint16_t u16RemTime = MAX_WARMUP_TIME_IN_SECS;
if((true == isWarmUpStarted) && (u16RemTime > 0))
{
u16RemTime--;
objptrSplashScreen->SetTime(u16RemTime);
}
else
{
//Still running
qWarning("\n\n\n\n\n WARM UP TIMER RUNNING \n\n\n\n\n");
killTimer(warmUpTimerID);
}
}
Upvotes: 5
Reputation: 1509
If you use a QTimer
or QBasicTimer
, you can call the stop() on one of those.
Upvotes: 0