what to do during long calculation?
Hi all,
I have a QSpinBox which is connected to some slot to make some calculations:
Code:
connect(mySpinBox, SIGNAL(valueChanged(int) ), this, SLOT(calculate(int)));
The calculation takes some decent amount of time (~5s) and therefore the GUI freezes. Even worse, if I click once on the SpinBox, the value is actually increased 3 or 4 times.
Any hints on how to solve this ugly behaviour?
Greets,
Weilor
Re: what to do during long calculation?
The program freezes since the CPU is busy on an application process, and cannot process the GUI too in the meanwhile.
To don't make stop the GUI, it is possible to execute calculate(int) on a parallel thread... but you better check if that operation worths the work...
While for the incrementations, clicks are buffered while the process calculate(int) proceeds, to execute their events at the end of calculate(int). But since you said you click once....well, now I have no idea.
Hope it helps
Greetings
Re: what to do during long calculation?
Thanks for your answer,
Is it such an effort to execute some operation in another thread? Maybe you can give me some pseudo-code how to do it?
Greets,
weilor
Re: what to do during long calculation?
I am not sure about details of Qt4, but in Qt3 was quite easy.
You have to inherit a your class from QThread and there override the virtual function run() that is called by the QThread start()method.
An example could be:
Code:
{
private:
. . .
public:
// constructor
virtual void run(); //inside this you execute your calculate(int)
. . .
}
for more details that sure you need, refer to http://doc.trolltech.com/4.3/qthread.html
Anyway, calculate from thread is a bit more complicated and could be a risk, since unxpected terminations could crash your program. Complicated because you have to manage the thread result to send it to your application, with some wrappers.
So I suggest you to well study the thread guide. ;)
Greetings
Re: what to do during long calculation?
You don't have to use threads. You can either ask the queue to process its events somewhere during your calculations using QCoreApplication::processEvents() or divide your calculations into smaller chunks and use QTimer::singleShot with 0 timeout to fire next stage at the end of previous stage of calculations.