I have created an application with a countdown timer and the number of seconds remaining is displayed in a QLabel widget inside the Graphics View Framework.
In the code below time_label is a QLabel and time_bar is a QProgressBar. Both widgets are wrapped inside a QGraphicsProxyWidgets which are displayed on a QGraphicsScene. The method timerEvent is triggered every 1000ms. The onTestOver() method kills the timer once the time is up. I had left out the irrelevant code on purpose.
{
time_bar->setValue(time_bar->value()+1);
int max = time_bar->maximum();
int val = time_bar->value();
int time_left = max - val;
qDebug() << time_left;
if (time_left > 0)
{
time_label->setText(tr("%1 seconds remaining").arg(time_left));
}
else
{
time_label->setText(tr("Time is up!"));
onTestOver();
}
}
void TestPlan::timerEvent(QTimerEvent* tick)
{
time_bar->setValue(time_bar->value()+1);
int max = time_bar->maximum();
int val = time_bar->value();
int time_left = max - val;
qDebug() << time_left;
if (time_left > 0)
{
time_label->setText(tr("%1 seconds remaining").arg(time_left));
}
else
{
time_label->setText(tr("Time is up!"));
onTestOver();
}
}
To copy to clipboard, switch view to plain text mode
In Qt Creator's "Application Output" I can see that timerEvent() is called every second so the timer works fine:
14
13
12
11
10
9
8
7
6
5
4
3
2
1
0
14
13
12
11
10
9
8
7
6
5
4
3
2
1
0
To copy to clipboard, switch view to plain text mode
However the output on the application whether using Debug or Release build I see the count down from 14 to 12, 11 is skipped, then from 10 to 5, 4 is skipped again, and the from 3 to 0.
I though maybe the QLabel updates slowly and I tried in increase the time per interval to 2000ms, but had the same result. The progressbar which also updates by this method, updates correctly every second. It is just the QLabel that sometimes seems to skip an update.
Any ideas on how I can make the QLabel widget to update correctly?
Bookmarks