- Extending Qt Creator Manual
- TaskTree
- QTaskTree
- Extending Qt Creator Manual 19.0.2

# QTaskTree Class

The QTaskTree class runs the tree of asynchronous tasks defined in a declarative way. More...

Header: | `#include <qtasktree.h>`

- List of all members, including inherited members

Note: All functions in this class are reentrant.

## Public Functions

void | onStorageDone(const QtTaskTree::Storage<StorageStruct> &storage, Handler &&handler)
void | onStorageSetup(const QtTaskTree::Storage<StorageStruct> &storage, Handler &&handler)
int | progressMaximum() const

## Signals

void | asyncCountChanged(int count)
void | done(QtTaskTree::DoneWith result)
void | progressValueChanged(int value)
void | started()

## Detailed Description

The description on what the task tree should do is passed via recipe.

## Member Function Documentation

### `[signal]` void QTaskTree::asyncCountChanged(int count)

This signal is emitted when the running task tree is about to return control to the caller's event loop. When the task tree is started, this signal is emitted with count value of `0`, and emitted later on every asyncCount() value bump with an updated count value. Every signal sent (except the initial one with the value of `0`) guarantees that the task tree is still running asynchronously after the emission.

See also asyncCount().

### `[signal]` void QTaskTree::done(QtTaskTree::DoneWith result)

This signal is emitted when the task tree finished, passing the final result of the execution. The task tree neither calls any handler, nor emits any signal anymore after this signal was emitted.

Note: Do not delete the task tree directly from this signal's handler. Use deleteLater() instead.

See also started().

### template <typename StorageStruct, typename Handler> void QTaskTree::onStorageDone(const QtTaskTree::Storage<StorageStruct> &storage, Handler &&handler)

Installs a storage done handler for the storage to retrieve the final data dynamically from the running task tree.

The `StorageHandler` takes a `const` reference to the `StorageStruct` instance:

     static QByteArray load(const QString &fileName) { ... }

     Storage<QByteArray> storage;

     const auto onLoaderSetup = [](QThreadFunction<QByteArray> &task) {
         task.setThreadFunctionData(&load, "foo.txt");
     };
     const auto onLoaderDone = [storage](const QThreadFunction<QByteArray> &task) {
         *storage = task.result();
     };

     const Group root {
         storage,
         QThreadFunctionTask(onLoaderSetup, onLoaderDone, CallDone::OnSuccess)
     };

     QTaskTree taskTree(root);
     auto collectStorage = [](const QByteArray &storage){
         qDebug() << "final content" << storage;
     };
     taskTree.onStorageDone(storage, collectStorage);
     taskTree.start();

When the running task tree is about to leave a Group where the storage is placed in, it destructs a `StorageStruct` instance. Just before the `StorageStruct` instance is destructed, and after all possible handlers from this group were called, the task tree invokes the passed handler. This enables reading the final content of the given storage dynamically and processing it further outside of the task tree.

This handler is called also when the running tree is canceled. However, it's not called when the running tree is destructed.

See also onStorageSetup().

### template <typename StorageStruct, typename Handler> void QTaskTree::onStorageSetup(const QtTaskTree::Storage<StorageStruct> &storage, Handler &&handler)

Installs a storage setup handler for the storage to pass the initial data dynamically to the running task tree.

The `StorageHandler` takes a reference to the `StorageStruct` instance:

     static void save(const QString &fileName, const QByteArray &array) { ... }

     Storage<QByteArray> storage;

     const auto onSaverSetup = [storage](QThreadFunction<QByteArray> &task) {
         task.setThreadFunctionData(&save, "foo.txt", *storage);
     };

     const Group root {
         storage,
         QThreadFunctionTask(onSaverSetup)
     };

     QTaskTree taskTree(root);
     auto initStorage = [](QByteArray &storage){
         storage = "initial content";
     };
     taskTree.onStorageSetup(storage, initStorage);
     taskTree.start();

When the running task tree enters a Group where the storage is placed in, it creates a `StorageStruct` instance, ready to be used inside this group. Just after the `StorageStruct` instance is created, and before any handler of this group is called, the task tree invokes the passed handler. This enables setting up initial content for the given storage dynamically. Later, when any group's handler is invoked, the task tree activates the created and initialized storage, so that it's available inside any group's handler.

See also onStorageDone().

### int QTaskTree::progressMaximum() const

Returns the maximum progressValue().

Note: Currently, it's the same as taskCount(). This might change in the future.

See also progressValue().

### `[signal]` void QTaskTree::progressValueChanged(int value)

This signal is emitted when the running task tree finished, canceled, or skipped some tasks. The value gives the current total number of finished, canceled or skipped tasks. When the task tree is started, and after the started() signal was emitted, this signal is emitted with an initial value of `0`. When the task tree is about to finish, and before the done() signal is emitted, this signal is emitted with the final value of progressMaximum().

See also progressValue() and progressMaximum().

### `[signal]` void QTaskTree::started()

This signal is emitted when the task tree is started. The emission of this signal is followed synchronously by the progressValueChanged() signal with an initial `0` value.

See also start() and done().