当它正在观看的QFutureWatcher类完成时,finished()类会发出信号finished()。我怎么能看多个QFuture?我正在使用QtConcurrent::run()并行运行两个线程,希望在两个线程完成后获得一个信号。
发布于 2020-10-31 11:33:09
我会这样处理这个问题:
QFutureWatcherQFutureWatchers添加到列表中,resp。向量,例如m_futureWatchers:
m_futureWatchers.append(futureWatcher);QFutureWatcher::finished信号连接到同一个时隙,例如handleFinished:
连接(futureWatcher,&QFutureWatcher::connect,this,MyClass::handleFinished);handleFinished槽中,检查QFutureWatcher::isFinished并作出相应反应:
bool allAreFinished = true;for (auto*futureWatcher: m_futureWatchers) allAreFinished &= futureWatcher->isFinished();if (allAreFinished) { // doSomething }注意:对于两个未来的观察者来说,可能更容易一些,比如有两个成员变量,例如m_futureWatcher1和m_futureWatcher1,而不是一个列表,然后像这样在handleFinished槽中检查它们:
if (m_futureWatcher1->isFinished() && m_futureWatcher2->isFinished) {
...
}发布于 2020-10-31 11:44:15
您可以为Qt使用第三方AsyncFuture库:
将具有不同类型的多个期货组合成一个未来的单一对象:
/* Combine multiple futures with different type into a single future */
QFuture<QImage> f1 = QtConcurrent::run(readImage, QString("image.jpg"));
QFuture<void> f2 = observe(timer, &QTimer::timeout).future();
QFuture<QImage> result = (combine() << f1 << f2).subscribe([=](){
// Read an image but do not return before timeout
return f1.result();
}).future();
QCOMPARE(result.progressMaximum(), 2);https://stackoverflow.com/questions/64620530
复制相似问题