QFuture Class

The QFuture class represents the result of an asynchronous computation. More...

Header: #include <QFuture>
qmake: QT += core
Since: Qt 4.4

Note: All functions in this class are thread-safe with the following exceptions:

Public Types

Public Functions

QFuture()
QFuture(QFutureInterface<T> *p)
QFuture(const QFuture<T> &)
~QFuture()
QFuture::const_iterator begin() const
void cancel()
QFuture::const_iterator constBegin() const
QFuture::const_iterator constEnd() const
QFuture::const_iterator end() const
bool isCanceled() const
bool isFinished() const
bool isPaused() const
bool isResultReadyAt(int resultIndex) const
bool isRunning() const
bool isStarted() const
void pause()
int progressMaximum() const
int progressMinimum() const
QString progressText() const
int progressValue() const
T result() const
T resultAt(int index) const
int resultCount() const
QList<T> results() const
void resume()
void setPaused(bool paused)
void togglePaused()
void waitForFinished()
T operator T() const
bool operator!=(const QFuture<T> &other) const
QFuture<T> &operator=(const QFuture<T> &)
bool operator==(const QFuture<T> &other) const

Public Variables

QFutureInterface<T> d

Detailed Description

The QFuture class represents the result of an asynchronous computation.

To start a computation, use one of the APIs in the Qt Concurrent framework.

QFuture allows threads to be synchronized against one or more results which will be ready at a later point in time. The result can be of any type that has a default constructor and a copy constructor. If a result is not available at the time of calling the result(), resultAt(), or results() functions, QFuture will wait until the result becomes available. You can use the isResultReadyAt() function to determine if a result is ready or not. For QFuture objects that report more than one result, the resultCount() function returns the number of continuous results. This means that it is always safe to iterate through the results from 0 to resultCount().

QFuture provides a Java-style iterator (QFutureIterator) and an STL-style iterator (QFuture::const_iterator). Using these iterators is another way to access results in the future.

QFuture also offers ways to interact with a runnning computation. For instance, the computation can be canceled with the cancel() function. To pause the computation, use the setPaused() function or one of the pause(), resume(), or togglePaused() convenience functions. Be aware that not all asynchronous computations can be canceled or paused. For example, the future returned by QtConcurrent::run() cannot be canceled; but the future returned by QtConcurrent::mappedReduced() can.

Progress information is provided by the progressValue(), progressMinimum(), progressMaximum(), and progressText() functions. The waitForFinished() function causes the calling thread to block and wait for the computation to finish, ensuring that all results are available.

The state of the computation represented by a QFuture can be queried using the isCanceled(), isStarted(), isFinished(), isRunning(), or isPaused() functions.

QFuture is a lightweight reference counted class that can be passed by value.

QFuture<void> is specialized to not contain any of the result fetching functions. Any QFuture<T> can be assigned or copied into a QFuture<void> as well. This is useful if only status or progress information is needed - not the actual result data.

To interact with running tasks using signals and slots, use QFutureWatcher.

See also QFutureWatcher and Qt Concurrent.

Member Type Documentation

typedef QFuture::ConstIterator

Qt-style synonym for QFuture::const_iterator.

Member Function Documentation

QFuture::QFuture()

Default constructs an instance of QFuture.

QFuture::QFuture(QFutureInterface<T> *p)

Default constructs an instance of QFuture.

QFuture::QFuture(const QFuture<T> &)

Default constructs an instance of QFuture.

QFuture::~QFuture()

Destroys the instance of QFuture.

QFuture::const_iterator QFuture::begin() const

void QFuture::cancel()

QFuture::const_iterator QFuture::constBegin() const

QFuture::const_iterator QFuture::constEnd() const

QFuture::const_iterator QFuture::end() const

bool QFuture::isCanceled() const

bool QFuture::isFinished() const

bool QFuture::isPaused() const

bool QFuture::isResultReadyAt(int resultIndex) const

bool QFuture::isRunning() const

bool QFuture::isStarted() const

void QFuture::pause()

int QFuture::progressMaximum() const

int QFuture::progressMinimum() const

QString QFuture::progressText() const

int QFuture::progressValue() const

T QFuture::result() const

T QFuture::resultAt(int index) const

int QFuture::resultCount() const

QList<T> QFuture::results() const

void QFuture::resume()

void QFuture::setPaused(bool paused)

See also isPaused().

void QFuture::togglePaused()

void QFuture::waitForFinished()

T QFuture::operator T() const

bool QFuture::operator!=(const QFuture<T> &other) const

QFuture<T> &QFuture::operator=(const QFuture<T> &)

Copy-assignment operator.

bool QFuture::operator==(const QFuture<T> &other) const