Re: How to refresh GUI while heavy processing is ongoing?
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."
Re: How to refresh GUI while heavy processing is ongoing?
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();
Re: How to refresh GUI while heavy processing is ongoing?
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!
Re: How to refresh GUI while heavy processing is ongoing?
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 for further informations.
Re: How to refresh GUI while heavy processing is ongoing?
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.
Re: How to refresh GUI while heavy processing is ongoing?
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.