views:

160

answers:

3

My first naive at updating my progress bar was to include the following lines in my loop which is doing the processing, making something like this:

while(data.hasMoreItems())
{
    doSomeProcessing(data.nextItem())

    //Added these lines but they don't do anything
    ui->progressBar->setValue(numberProcessed++);
    ui->progressBar->repaint();
}

I thought adding the repaint() would make the execution pause while it updated the GUI, but apparently it's not that simple. After looking at the questions:

QProgressBar Error
Progress bar is not showing progress

it looks like I'm going to have to put the data processing in a different thread and then connect a signal from the data processing thread to the GUI thread to update the progressbar. I'm rather inexperienced with GUIs and threads and I was wondering if anyone could just point me in the right direction, ie what Qt classes should I be looking at using for this. I'd guess I need a QThread object but I've been looking through the QProgressBar documentation but it doesn't bring up the topic of threading.

+1  A: 

You can create a sub-class of QThread that emits a signal progressChanged, which you connect to the QProgressBar.

connect() makes the connections auto connections per default. That means that the signal-slot-mechanism already takes care of the threading issues for you, so you don't need to worry about that.

Georg Fritzsche
+2  A: 

You need to call QApplication::processEvents() periodically inside your processing loop to let it handle UI events.

As Georg says, Qt is a single-threaded co-operative multitasking environment. You get full control of your process until you relinquish it voluntarily with processEvents() - until you do that, Qt can't update the UI elements, handle async HTTP requests, handle input, or pretty much anything else. It's up to you to make sure that stuff gets a timeslice while you're in a long processing loop.

rjh
+4  A: 

As @rjh and @Georg have pointed out, there are essentially two different options:

  1. Force processing of events using QApplication::processEvents(), OR
  2. Create a thread that emits signals that can be used to update the progress bar

If you're doing any non-trivial processing, I'd recommend moving the processing to a thread.

The most important thing to know about threads is that except for the main GUI thread (which you don't start nor create), you can never update the GUI directly from within a thread.

The last parameter of QObject::connect() is a Qt::ConnectionType enum that by default takes into consideration whether threads are involved.

Thus, you should be able to create a simple subclass of QThread that does the processing:

class DataProcessingThread : public QThread
 {

 public:
     void run();
 signals:
     void percentageComplete(int);
 };

 void MyThread::run()
 {
    while(data.hasMoreItems())
    {
      doSomeProcessing(data.nextItem())
      emit percentageCompleted(computePercentageCompleted());
    }
 }

And then somewhere in your GUI code:

DataProcessingThread dataProcessor(/*data*/);
connect(dataProcessor, SIGNAL(percentageCompleted(int)), progressBar, SLOT(setValue(int));
dataProcessor.start();
Kaleb Pederson