Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用QFuture更新QProgressDialog_C++_Qt_Qt4_Qtconcurrent - Fatal编程技术网

C++ 使用QFuture更新QProgressDialog

C++ 使用QFuture更新QProgressDialog,c++,qt,qt4,qtconcurrent,C++,Qt,Qt4,Qtconcurrent,在等待QFuture时,主GUI线程更新QProgressDialog的正确方式是什么。具体来说,在这个循环中会发生什么: QProgressDialog pd(...); QFuture f = ...; while (!f.isFinished()) { pd.setValue(f.progressValue()); // what goes here? } 现在我有一个类似sleep()的调用,但这并不是最优的(当然也会引入一些GUI延迟) 如果我什么也不放,主线程将循环po

在等待QFuture时,主GUI线程更新QProgressDialog的正确方式是什么。具体来说,在这个循环中会发生什么:

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

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

  // what goes here?
}
现在我有一个类似sleep()的调用,但这并不是最优的(当然也会引入一些GUI延迟)

如果我什么也不放,主线程将循环pole pd.setValue(),浪费CPU周期

我希望放置类似于QCoreApplication::processEvents(标志,maxtime)的东西,但是如果事件队列为空,它会立即返回。我想要一个版本,等待,直到maxtime或任何东西,即使队列是空的。这样,我得到了延迟,主线程总是准备好响应GUI事件。

使用信号和插槽来监视QFuture对象

QFutureWatcher watcher;
QProgressDialog pd(...);
connect(&watcher, SIGNAL(progressValueChanged(int)), &pd, SLOT(setValue(int)));
QFuture f = ...
watcher.setFuture(f);

回答得好,蒂姆。仅供参考,
connect(watcher,…)中的
watcher
前面是否缺少一个
&