本页内容

QtTaskTree::QSyncTask Class

class QtTaskTree::QSyncTask

在其他任务之间同步执行自定义处理程序。更多...

标题: #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
状态: 技术预览

该类处于技术预览阶段,内容可能会有变动。

注意:此类中的所有函数均为可重入的。

公共函数

QSyncTask(Handler &&handler)

详细说明

当您希望在其他任务之间执行一个额外的处理程序时,QSyncTask 非常有用。对其父类Group 而言,QSyncTask 与其他任务无异。请避免让 QSyncTask 的处理程序主体长时间运行,因为它会从调用线程同步执行。如果无法避免,请考虑改用QThreadFunctionTask 。

成员函数文档

[explicit] template <typename Handler, std::enable_if_t<!std::is_same_v<q20::remove_cvref_t<Handler>, QSyncTask>, bool> = true> QSyncTask::QSyncTask(Handler &&handler)

构建一个元素,该元素会同步执行传入的handler 。Handler 的类型为std::function<DoneResult()> 。由handler 返回的DoneResult 值,将在父组的workflow policy 解析过程中被考虑。此外,也支持std::function<void()> 的简写形式。在此情况下,默认假设其返回值为DoneResult::Success 。

传入的handler 将从调用线程同步执行,因此应避免处理程序主体的长时间运行。否则,请考虑使用QThreadFunctionTask 。

注意: 在报告任务树进度时,QSyncTask 元素不被计入任务数量,也不会包含在QTaskTree::taskCount() 或QTaskTree::progressMaximum() 中。

© 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.