QtTaskTree::Group Class
class QtTaskTree::Group组是组成声明式配方的基本元素,用于描述如何执行和处理嵌套的异步任务树。更多
| 页眉: | #include <qtasktree.h> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS TaskTree)target_link_libraries(mytarget PRIVATE Qt6::TaskTree) |
| qmake: | QT += tasktree |
| 自 | Qt 6.11 |
| 继承: | QtTaskTree::ExecutableItem |
注意:该类中的所有函数都是可重入的。
公共函数
| Group(const QtTaskTree::GroupItems &children) | |
| Group(std::initializer_list<QtTaskTree::GroupItem> children) |
详细说明
组是其他组项的容器。它将子任务封装为一个单元,而组的父类将其视为一个单独的异步任务。由于 Group 属于GroupItem 类型,因此它也可以是 Group 的子任务。
通过使用别名的自定义任务名称,如QThreadFunctionTask<ResultType> 或QNetworkReplyWrapperTask ,将子任务插入组中:
const Group group { QNetworkReplyWrapperTask(...), QThreadFunctionTask<int>(...) };
您可以通过插入ExecutionMode 或WorkflowPolicy 项来定制组的行为:
const Group group { parallel, continueOnError, QNetworkReplyWrapperTask(...), QNetworkReplyWrapperTask(...) };
组可包含嵌套组:
const Group group { finishAllAndSuccess, QNetworkReplyWrapperTask(...), Group { QNetworkReplyWrapperTask(...), Group { parallel, QNetworkReplyWrapperTask(...), QNetworkReplyWrapperTask(...), } QThreadFunctionTask<QString>(...) } };
组可动态实例化自定义存储结构,该结构可用于任务间数据交换:
struct MyCustomStruct { QByteArray data; }; Storage<MyCustomStruct> storage; const auto onFirstSetup = [](QNetworkReplyWrapper &task) { ... }; const auto onFirstDone = [storage](const QNetworkReplyWrapper &task) { // storage-> gives a pointer to MyCustomStruct instance, // created dynamically by the running task tree. storage->data = task.reply()->readAll(); }; const auto onSecondSetup = [storage](QThreadFunction<QImage> &task) { // storage-> gives a pointer to MyCustomStruct. Since the group is sequential, // the stored MyCustomStruct was already updated inside the onFirstDone handler. const QByteArray storedData = storage->data; }; const Group group { // When the group is entered by a running task tree, it creates MyCustomStruct // instance dynamically. It is later accessible from all handlers via // the *storage or storage-> operators. sequential, storage, QNetworkReplyWrapperTask(onFirstSetup, onFirstDone, CallDoneFlag::OnSuccess), QThreadFunctionTask<QImage>(onSecondSetup) };
成员函数文档
Group::Group(const QtTaskTree::GroupItems &children)
用给定的children 列表构造一个组。
如果在编译时不知道组的子项,而在运行时才知道,那么这个构造函数就很有用:
const QStringList sourceList = ...; GroupItems groupItems { parallel }; for (const QString &source : sourceList) { const QNetworkReplyWrapperTask task(...); // use source for setup handler groupItems << task; } const Group group(groupItems);
Group::Group(std::initializer_list<QtTaskTree::GroupItem> children)
通过children 从std::initializer_list 构造一个组。
当编译时已知道组的所有子项时,该构造函数就非常有用:
const Group group { finishAllAndSuccess, QNetworkReplyWrapperTask(...), Group { QNetworkReplyWrapperTask(...), Group { parallel, QNetworkReplyWrapperTask(...), QNetworkReplyWrapperTask(...), } QThreadFunctionTask<QString>(...) } };
© 2026 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.