PDA

View Full Version : how to use prograssbar in a thread



zl2k
18th September 2008, 19:12
hi, there
I have a main program and from which dispatch 2 threads. The program within each thread is a loop and I do it in sequential. I need to use 2 prograssbar for each of the 4 thread by counting the steps if the loop. I tried to implement it but failed. (It's fine if not using thread). Can someone give me a hint? Thanks a lot.

I also have a prograssbar to monitor the threads. Each thread will send out signal to increase the bar when it is finshed. Again, this prograssbar's behavior is weird: when progressbar value changed, the bar did not increase immediately. At the end when all threads are finished, the bar shows 100% in text, but the bar itself is only 1/2 full.
zl

jpn
18th September 2008, 19:19
You must not touch GUI in worker threads. Use queued signals to deliver progress to the main thread. For example:



class MyThread : public QThread
{
signals:
void valueChanged(int value);
};

connect(thread, SIGNAL(valueChanged(int)), progressBar, SLOT(setValue(int)));

zl2k
18th September 2008, 19:29
Can I create the progressbar within the work thread?
Or should I creat the progressbar outside of the work thread (in it's parent module) and let the work thread signal the progressbar?

jpn
18th September 2008, 19:34
Can I create the progressbar within the work thread?
Or should I creat the progressbar outside of the work thread (in it's parent module) and let the work thread signal the progressbar?
No, you cannot create widgets within worker threads. Don't make the worker thread aware of any progress bars at all. Just emit signals about the progress. Establish the connection in the GUI thread where you have access to both receiver and sender.