PDA

View Full Version : refreshing labels



ht1
29th December 2007, 20:34
Hi,

I was wondering why my label does not automatically update while this is running:



for (int kk=1; kk<100; kk++)
{
//main code is here
Label->setNum(kk);
}

Label is a QLabel declared elsewhere

I'd like to see the numbers counted between 1 and 99 but the only number I see is 99 (when the program is complete). Why don't I see other numbers sequentially on the screen. Is it because labels can be refreshed only when control is returned to main()?
Can labels be refreshed while another function is running?

marcel
29th December 2007, 20:44
You set the numbers too fast. The label doesn't get the chance to update.
You should use a QTimer with an interval of 100-200ms. Connect its timeout signal to a slot in which you update the label with a new value. Then you will be able to see something.

ht1
29th December 2007, 20:57
Thanks Marcel, but I think something else than speed is the problem.
The code that I designated as (//main code here) is very long and takes a long time to compile. The labels have about a minute to refresh. Maybe the labels don't refresh because the processor is 100% busy with doing the math?

jacek
29th December 2007, 21:01
Maybe the labels don't refresh because the processor is 100% busy with doing the math?
Yes, you block the Qt's event loop and Qt can't redraw anything or even process user's input. You can avoid this by invoking QCoreApplication::processEvents() from time to time or using QTimer to drive your loop. A third option is to move those computations to a thread.

jpn
29th December 2007, 21:02
In other words, events don't get processed during a busy loop. When you change the contents of a label, a few events get scheduled (to update the label). But no event reaches its receiver until you return the control to the event loop. One simple but a bit ugly solution is to force the application to process its events:


for (int kk=1; kk<100; kk++)
{
//main code is here
Label->setNum(kk);
QApplication::processEvents(); // <--
}