PDA

View Full Version : How to refresh GUI while heavy processing is ongoing?



falconium
12th May 2011, 17:51
Hi,

I'm extracting and processing XML files that causes high CPU occupancy. This causes GUI freezing, so no update is occurring for neither progress bar nor for other elements.

void NESA::quickProcess()
{
qApp->processEvents();
}


I've put the following into the constructor:

timer = new QTimer(this);
timer->setInterval(1000);
connect(timer, SIGNAL(timeout()), this, SLOT(quickProcess()));

And before heavy computing I call timer.start();, and when it finishes I call timer.stop();

However, it is still the same. Any hint? Thanks!

Added after 16 minutes:

Does timer start a new thread, is this the reason?
"Calling this function processes events only for the calling thread."

Santosh Reddy
12th May 2011, 17:56
If you want the GUI to keep on updating / refreshing, the event loop should continue to loop, while the quickProcess() is running, I think you either need to put quickProcess() in a QThread, or divide quickProcess() into samller blocks and call them in timerEvents();

falconium
12th May 2011, 20:36
Hi Santosh,

Thanks for the reply!
I think I was not clear enough. quickProcess() is for processing all the remaining pending events in the loop. There is another, separate function for doing calculations. Do you think putting that into a thread could help?
Should I call timer.start() inside that thread, or should I use a totally different approach?

Thanks!

Lykurg
12th May 2011, 21:50
During the "heavy computing" no events are processed, so your timer is useless. Either call processEvents() inside your computation or move the hole computation inside a thread or a separate event loop.

See this article (http://www.qtcentre.org/wiki/index.php?title=Keeping_the_GUI_Responsive) for further informations.

falconium
12th May 2011, 22:08
Thanks Lykurg!
I have been using processEvents with no user interaction flag inside the computing loop, but it made it very-very slow, so my intention is to call processEvents only occasionally to have a win-win situation. If it is not possible to call a function with timer (as they are also called inside event loop), then I'm going to check the link you included.

squidge
12th May 2011, 23:35
Typically I run as little code as possible in the main ui event loop to try and keep it as responsive as possible. Anything that can take >= 250ms per 5 seconds I place into another thread. With signals and slots, Qt makes it really easy to communicate between threads, so all the ui does is react to events and signals.