6.0.0-rc4Source

QFutureWatcher Class

template <typename T> class QFutureWatcher

The QFutureWatcher class allows monitoring a QFuture using signals and slots. More…

Header#include <QFutureWatcher>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes
InheritsQFutureWatcherBase

List of all members, including inherited members

Public Functions

QFutureWatcher(QObject *parent = nullptr)
~QFutureWatcher()
voidsetFuture(const QFuture<T> &future)
QFuture<T>future() const
Uresult() const
UresultAt(int index) const

Detailed Description

A watcher turns what a future does into signals: started(), resultReadyAt(), progressValueChanged(), canceled(), finished() and more. The signals come from the event loop of the thread that the watcher lives in, whichever thread reports, so slots can touch the objects of that thread.

QFutureWatcher<int> watcher;
QObject::connect(&watcher, &QFutureWatcherBase::resultReadyAt, [&](int i) { show(watcher.resultAt(i)); });
QObject::connect(&watcher, &QFutureWatcherBase::finished, &loop, &QEventLoop::quit);
watcher.setFuture(future);

The slots cancel(), suspend() and resume() control the watched computation. A watcher can be moved to another thread with QObject::moveToThread(), which takes the signals that are on their way along. Destroy a watcher before its thread's event loop ends.

See also QFuture, QPromise and QFutureSynchronizer.

Differences from Qt

The signals are not connected lazily: the watcher always listens to its future. The class has no QFutureWatcher<void> specialization; the functions that give results are simply absent for it. Results and progress that were reported before a cancellation, but not delivered, are dropped, as in Qt. The signals paused() and suspending() are both emitted when a suspension is requested.

Member Function Documentation

[explicit] QFutureWatcher::QFutureWatcher(QObject *parent = nullptr)

Constructs a watcher with the given parent. It watches no future until setFuture() is called.

[override] QFutureWatcher::~QFutureWatcher()

Destroys the watcher. Signals that were not delivered yet are dropped.

QFuture<T> QFutureWatcher::future() const

Returns the watched future, which is invalid if there is none.

See also setFuture().

template <typename U = T, typename = std::enable_if_t<!std::is_void_v<U>>>U QFutureWatcher::result() const

Waits for the first result of the watched future and returns a copy of it.

See also QFuture::result().

template <typename U = T, typename = std::enable_if_t<!std::is_void_v<U>>>U QFutureWatcher::resultAt(int index) const

Waits for the result at index of the watched future and returns a copy of it.

See also QFuture::resultAt().

void QFutureWatcher::setFuture(const QFuture<T> &future)

Starts watching future, and stops watching the previous one. Events for what the future did already, its start, results, progress and end, are delivered from the event loop as if the watcher had been there from the start. It does nothing if the watcher watches future already.

See also future().