IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

QtFuture Namespace

Contains miscellaneous identifiers used by the QFuture class.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

QtFuture Namespace

  • Header: QFuture

  • CMake:

    find_package(Qt6 REQUIRED COMPONENTS Core)

    target_link_libraries(mytarget PRIVATE Qt6::Core)

  • qmake: QT += core

Detailed Description

 

Type Documentation

 

[since 6.0] enum QtFuture::Launch

Represents execution policies for running a QFuture continuation.

Constant

Value

Description

QtFuture::Launch::Sync

0

The continuation will be launched in the same thread that fulfills the promise associated with the future to which the continuation was attached, or if it has already finished, the continuation will be invoked immediately, in the thread that executes then().

QtFuture::Launch::Async

1

The continuation will be launched in a separate thread taken from the global QThreadPool.

QtFuture::Launch::Inherit

2

The continuation will inherit the launch policy or thread pool of the future to which it is attached.

Sync is used as a default launch policy.

This enum was introduced or modified in Qt 6.0.

See Also

Function Documentation

 

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:

 
Sélectionnez
class Object : public QObject
{
    Q_OBJECT
    ...
signals:
    void noArgSignal();
    void singleArgSignal(int value);
    void multipleArgs(int value1, double value2, const QString &amp;value3);
};

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

 
Sélectionnez
Object object;
QFuture&lt;void&gt; voidFuture = QtFuture::connect(&amp;object, &amp;Object::noArgSignal);
QFuture&lt;int&gt; intFuture = QtFuture::connect(&amp;object, &amp;Object::singleArgSignal);

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

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

 
Sélectionnez
QtFuture::connect(&amp;object, &amp;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:

 
Sélectionnez
QtFuture::connect(&amp;object, &amp;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:

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

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

See Also

See also QFuture, QFuture::then()

[since 6.1] QFuture<T> QtFuture::makeExceptionalFuture(const QException &exception)

Creates and returns a QFuture which already has an exception exception.

 
Sélectionnez
QException e;
auto f = QtFuture::makeExceptionalFuture&lt;int&gt;(e);
...
try {
    f.result(); // throws QException
} catch (QException &amp;) {
    // handle exception here
}

This function was introduced in Qt 6.1.

See Also

[since 6.1] QFuture<T> QtFuture::makeExceptionalFuture(std::exception_ptr exception)

This is an overloaded function.

Creates and returns a QFuture which already has an exception exception.

 
Sélectionnez
struct TestException
{
};
...
auto exception = std::make_exception_ptr(TestException());
auto f = QtFuture::makeExceptionalFuture&lt;int&gt;(exception);
...
try {
    f.result(); // throws TestException
} catch (TestException &amp;) {
    // handle exception here
}

This function was introduced in Qt 6.1.

See Also

[since 6.1] QFuture<std::decay_t<T>> QtFuture::makeReadyFuture(T &&value)

This is an overloaded function.

Creates and returns a QFuture which already has a result value. The returned QFuture has a type of std::decay_t<T>, where T is not void.

 
Sélectionnez
auto f = QtFuture::makeReadyFuture(std::make_unique&lt;int&gt;(42));
...
const int result = *f.takeResult(); // result == 42

This function was introduced in Qt 6.1.

See Also

[since 6.1] QFuture<void> QtFuture::makeReadyFuture()

This is an overloaded function.

Creates and returns a void QFuture. Such QFuture can't store any result. One can use it to query the state of the computation. The returned QFuture will always be in the finished state.

 
Sélectionnez
auto f = QtFuture::makeReadyFuture();
...
const bool started = f.isStarted(); // started == true
const bool running = f.isRunning(); // running == false
const bool finished = f.isFinished(); // finished == true

This function was introduced in Qt 6.1.

See Also

[since 6.1] QFuture<T> QtFuture::makeReadyFuture(const QList<T> &values)

This is an overloaded function.

Creates and returns a QFuture which already has multiple results set from values.

 
Sélectionnez
const QList&lt;int&gt; values { 1, 2, 3 };
auto f = QtFuture::makeReadyFuture(values);
...
const int count = f.resultCount(); // count == 3
const auto results = f.results(); // results == { 1, 2, 3 }

This function was introduced in Qt 6.1.

See Also

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+