QtTaskTree::QCustomTask Class
template <typename Task, typename Adapter = QtTaskTree::QDefaultTaskAdapter<Task>, typename Deleter = std::default_delete<Task>> class QtTaskTree::QCustomTaskA class template used for declaring custom task items and defining their setup and done handlers. More...
| Header: | #include <qtasktree.h> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS TaskTree)target_link_libraries(mytarget PRIVATE Qt6::TaskTree) |
| qmake: | QT += tasktree |
| Since: | Qt 6.11 |
| Inherits: | QtTaskTree::ExecutableItem |
| Status: | Technical Preview |
Note: All functions in this class are reentrant.
Related Non-Members
Detailed Description
Describes custom task items within task tree recipes.
Custom task names are aliased with unique names using the QCustomTask template with a given Task, Adapter and Deleter. For example, QThreadFunctionTask<T> is an alias to the QCustomTask that is defined to work with QThreadFunction<T> as an associated task class. The following table contains custom tasks provided by the TaskTree library and their associated task classes:
| Aliased Task Name | Associated Task Class | Brief Description |
|---|---|---|
| QBarrierTask | QBarrier | Starts an awaiter task. |
| QThreadFunctionTask<ReturnType> | QThreadFunction<ReturnType> | Starts an asynchronous task. Runs in a separate thread. |
| QProcessTask | QProcess | Starts a process. |
| QNetworkReplyWrapperTask | QNetworkReplyWrapper | Sends a network query. |
| QTaskTreeTask | QTaskTree | Starts a nested task tree. |
| QTcpSocketWrapperTask | QTcpSocketWrapper | Establishes TCP connection. |
| QTimeoutTask | std::chrono::milliseconds | Starts a timer. |
The Task template parameter is mandatory, and specifies what type of Task the running QTaskTree will instantiate when it's a part of recipe. The Task type needs to be default constructible.
The Adapter template argument is optional. It may be skipped if QDefaultTaskAdapter<Task> is fine to adapt the Task type. Otherwise, the Adapter for certain Task needs to have the following form:
class Adapter { public: void operator()(Task *task, QTaskInterface *iface) { ... } };
Implement the operator() above to start the task, and call QTaskInterface::reportDone() method on the passed iface when the task is finished.
It is guaranteed the passed Task and QTaskInterface outlives the Adapter. If necessary, the destructor of Adapter may still access the passed Task.
For more details see QTaskInterface.
The Deleter template argument is optional. By default, the std::default_delete<Task> is used. The custom Deleter is useful when the destructor of the running Task may potentially block the caller thread. Instead of blocking, the custom deleter may move the running task into a separate thread and implement the blocking destruction there. In this way, the fast destruction (seen from the caller thread) of the running task with a blocking destructor may be achieved.
See also QTaskInterface.
Related Non-Members
[alias] QTaskTreeTask
Type alias for the QCustomTask<QTaskTree>, to be used inside recipes.
[alias] QTimeoutTask
Type alias for the QCustomTask<std::chrono::milliseconds>, to be used inside recipes. The std::chrono::milliseconds is used to set up the timeout duration. The default timeout is std::chrono::milliseconds::zero(), that is, the QTimeoutTask finishes as soon as the control returns to the running event loop.
Example usage:
using namespace std::chrono; using namespace std::chrono_literals; const auto onSetup = [](milliseconds &timeout) { timeout = 1000ms; } const auto onDone = [] { qDebug() << "Timed out."; } const Group root { QTimeoutTask(onSetup, onDone) };
See also timeoutTask().