开发者

Updating a QProgressDialog with a QFuture

开发者 https://www.devze.com 2022-12-14 04:08 出处:网络
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:

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.


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消