Question

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.

Was it helpful?

Solution

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top