QtFuture Namespace

Contains miscellaneous identifiers used by the QFuture class. More...

Header: #include <QFuture>
CMake: find_package(Qt6 COMPONENTS Core REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core


enum class Launch { Sync, Async, Inherit }


QFuture<ArgsType<Signal> > connect(Sender *sender, Signal signal)

Detailed Description

Type Documentation

[since 6.0] enum class QtFuture::Launch

Represents execution policies for running a QFuture continuation.

QtFuture::Launch::Sync0The continuation will be launched in the same thread in which the parent has been executing, or in the thread where the parent lives if the continuation is attached after the parent has already finished.
QtFuture::Launch::Async1The continuation will be launched in in a separate thread taken from the global QThreadPool.
QtFuture::Launch::Inherit2The continuation will inherit the launch policy of the parent or its thread pool, if it was using a custom one.

This enum was introduced or modified in Qt 6.0.

See also QFuture::then() and QThreadPool::globalInstance().

Function Documentation

template <typename Sender, typename Signal, typename> QFuture<ArgsType<Signal> > QtFuture::connect(Sender *sender, Signal signal)

Creates and returns a QFuture which will become available when the sender emits the signal. If the signal takes no arguments, a QFuture<void> is returned. If the signal takes a single argument, the resulted QFuture will be filled with the signal's argument value. If the signal takes multiple arguments, the resulted QFuture is filled with std::tuple storing the values of signal's arguments. If the sender is destroyed before the signal is emitted, the resulted QFuture will be canceled.

For example, let's say we have the following object:

class Object : public QObject
    void noArgSignal();
    void singleArgSignal(int value);
    void multipleArgs(int value1, double value2, const QString &value3);

We can connect its signals to QFuture objects in the following way:

Object object;
QFuture<void> voidFuture = QtFuture::connect(&object, &Object::noArgSignal);
QFuture<int> intFuture = QtFuture::connect(&object, &Object::singleArgSignal);

using Args = std::tuple<int, double, QString>;
QFuture<Args> tupleFuture = QtFuture::connect(&object, &Object::multipleArgs)

We can also chain continuations to be run when a signal is emitted:

QtFuture::connect(&object, &Object::singleArgSignal).then([](int value) {
    // do something with the value

You can also start the continuation in a new thread or a custom thread pool using QtFuture::Launch policies. For example:

QtFuture::connect(&object, &Object::singleArgSignal).then(QtFuture::Launch::Async, [](int value) {
    // this will run in a new thread

Throwing an exception from a slot invoked by Qt's signal-slot connection is considered to be an undefined behavior, if it is not handled within the slot. But with QFuture::connect(), you can throw and handle exceptions from the continuations:

QtFuture::connect(&object, &Object::singleArgSignal).then([](int value) {
    throw std::exception();
}).onFailed([](const std::exception &e) {
    // handle the exception
}).onFailed([] {
    // handle other exceptions

Note: The connected future will be fulfilled only once, when the signal is emitted for the first time.

See also QFuture and QFuture::then().

© 2021 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.