Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating a QProgressDialog with a QFuture

What's the proper way for the main GUI thread to update a QProgressDialog while waiting for a QFuture. Specifically, what goes in this loop:

QProgressDialog pd(...);
QFuture f = ...;

while (!f.isFinished()) {
  pd.setValue(f.progressValue());

  // what goes here?
}

Right now I have a sleep() like call there, but that's not optimal (and ofcourse introduces some GUI latency).

If I put nothing, the main thread will loop-pole pd.setValue(), wasting CPU cycles.

I was hoping of putting something like QCoreApplication::processEvents(flags,maxtime), but that returns immediately if the event queue is empty. I'd like a version that waits until maxtime or whatever even if the queue is empty. That way, I get my delay and the main thread is always ready to respond to GUI events.

like image 620
Alex77 Avatar asked Dec 10 '09 15:12

Alex77


1 Answers

Use a QFutureWatcher to monitor the QFuture object using signals and slots.

QFutureWatcher watcher;
QProgressDialog pd(...);
connect(&watcher, SIGNAL(progressValueChanged(int)), &pd, SLOT(setValue(int)));
QFuture f = ...
watcher.setFuture(f);
like image 158
TimW Avatar answered Sep 25 '22 05:09

TimW