18#include "moc_qgsfutureutils.cpp"
22QgsCombinedFutureHelper::QgsCombinedFutureHelper( QList<QFuture<void>> futures, QFuture<void> cancellationFuture, std::function<
void()> allFinishedCallback )
23 : mAllFinishedCallback( std::move( allFinishedCallback ) )
25 mWatchers.resize( futures.size() );
26 for ( qsizetype i = 0; i < futures.size(); i++ )
28 mWatchers[i] =
new QFutureWatcher<void>(
this );
29 connect( mWatchers[i], &QFutureWatcherBase::finished,
this, &QgsCombinedFutureHelper::onOneFinished );
30 mWatchers[i]->setFuture( futures[i] );
32 connect( &mCancellationWatcher, &QFutureWatcherBase::canceled,
this, &QgsCombinedFutureHelper::onCanceled );
33 mCancellationWatcher.setFuture( cancellationFuture );
36void QgsCombinedFutureHelper::onOneFinished()
41 for (
auto &watcher : std::as_const( mWatchers ) )
43 if ( !watcher->isFinished() )
47 mAllFinishedCallback();
51void QgsCombinedFutureHelper::onCanceled()
53 for (
auto &watcher : std::as_const( mWatchers ) )
63 if ( QThread::currentThread() == QCoreApplication::instance()->thread() )
73 QFutureWatcher<void> watcher;
74 QObject::connect( &watcher, &QFutureWatcherBase::finished, &loop, &QEventLoop::quit );
77 QCoreApplication::processEvents();
79 watcher.setFuture( future );
80 if ( !watcher.isFinished() )
83 future.waitForFinished();
88 future.waitForFinished();
94 auto watcher =
new QFutureWatcher<void>;
96 QFuture<void> cloned = QtFuture::connect( watcher, &QFutureWatcherBase::finished )
97 .then( [watcher]() { watcher->deleteLater(); } );
98 watcher->setFuture( base );
105#if QT_VERSION >= QT_VERSION_CHECK( 6, 10, 0 )
106 future.cancelChain();
static QFuture< void > clone(QFuture< void > base)
Returns a future that will finish at the same time base will.
static void cancelChain(QFuture< void > future)
Runs future.cancelChain() if supported by Qt version, future.cancel() otherwise.
static void waitForFinished(QFuture< void > future)
Waits for the future to become finished without blocking the main thread's event loop.