头: | #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 ) |
QFuture<T> | makeExceptionalFuture (const QException & exception ) |
QFuture<T> | makeExceptionalFuture (std::exception_ptr exception ) |
QFuture<std::decay_t<T> > | makeReadyFuture (T && value ) |
QFuture<void> | makeReadyFuture () |
QFuture<T> | makeReadyFuture (const QList<T> & 值 ) |
[since 6.0]
enum class QtFuture::
Launch
Represents execution policies for running a QFuture continuation.
常量 | 值 | 描述 |
---|---|---|
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.
该枚举在 Qt 6.0 引入 (或被修改)。
另请参阅 QFuture::then () 和 QThreadPool::globalInstance ().
创建并返回 QFuture which will become available when the sender 发射 signal 。若 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 { Q_OBJECT ... signals: 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 });
注意: The connected future will be fulfilled only once, when the signal is emitted for the first time.
另请参阅 QFuture and QFuture::then ().
[since 6.1]
template <typename T>
QFuture
<
T
> QtFuture::
makeExceptionalFuture
(const
QException
&
exception
)
创建并返回 QFuture which already has an exception exception .
QException e; auto f = QtFuture::makeExceptionalFuture<int>(e); ... try { f.result(); // throws QException } catch (QException &) { // handle exception here }
该函数在 Qt 6.1 引入。
另请参阅 QFuture , QException ,和 QtFuture::makeReadyFuture ().
[since 6.1]
template <typename T>
QFuture
<
T
> QtFuture::
makeExceptionalFuture
(
std::exception_ptr
exception
)
这是重载函数。
创建并返回 QFuture which already has an exception exception .
struct TestException { }; ... auto exception = std::make_exception_ptr(TestException()); auto f = QtFuture::makeExceptionalFuture<int>(exception); ... try { f.result(); // throws TestException } catch (TestException &) { // handle exception here }
该函数在 Qt 6.1 引入。
另请参阅 QFuture , QException ,和 QtFuture::makeReadyFuture ().
[since 6.1]
template <typename T, typename>
QFuture
<
std::decay_t
<
T
> > QtFuture::
makeReadyFuture
(
T
&&
value
)
这是重载函数。
创建并返回 QFuture which already has a result value . The returned QFuture has a type of std::decay_t<T>, where T is not void.
auto f = QtFuture::makeReadyFuture(std::make_unique<int>(42)); ... const int result = *f.takeResult(); // result == 42
该函数在 Qt 6.1 引入。
另请参阅 QFuture and QtFuture::makeExceptionalFuture ().
[since 6.1]
QFuture
<
void
> QtFuture::
makeReadyFuture
()
这是重载函数。
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.
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
该函数在 Qt 6.1 引入。
另请参阅 QFuture , QFuture::isStarted (), QFuture::isRunning (), QFuture::isFinished (),和 QtFuture::makeExceptionalFuture ().
[since 6.1]
template <typename T>
QFuture
<
T
> QtFuture::
makeReadyFuture
(const
QList
<
T
> &
值
)
这是重载函数。
创建并返回 QFuture which already has multiple results set from 值 .
const QList<int> 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 }
该函数在 Qt 6.1 引入。
另请参阅 QFuture and QtFuture::makeExceptionalFuture ().