- Extending Qt Creator Manual
- QtTaskTree
- When
- Extending Qt Creator Manual 21.0.0-beta1

# QtTaskTree::When Class

class QtTaskTree::When

An element delaying the execution of a body until barrier advance. More...

Header: | `#include <qbarriertask.h>`
CMake: | `find_package(Qt6 REQUIRED COMPONENTS TaskTree)` `target_link_libraries(mytarget PRIVATE Qt6::TaskTree)`
qmake: | `QT += tasktree`
Since: | 6.11

- List of all members, including inherited members

Note: All functions in this class are reentrant.

## Public Functions

| When(const QtTaskTree::BarrierKickerGetter &kicker, QtTaskTree::WorkflowPolicy policy = WorkflowPolicy::StopOnError)
| When(const QtTaskTree::QCustomTask<Task, Adapter, Deleter> &customTask, Signal signal, QtTaskTree::WorkflowPolicy policy = QtTaskTree::WorkflowPolicy::StopOnError)

## Related Non-Members

QtTaskTree::Group | operator>>(const QtTaskTree::When &whenItem, const QtTaskTree::Do &doItem)

## Detailed Description

When is used to delay the execution of a Do body until related QStoredBarrier is advanced or until QCustomTask's signal is emitted. It is used in conjunction with Do element like: `When () >> Do {}`.

See also Do.

## Member Function Documentation

### `[explicit]` When::When(const QtTaskTree::BarrierKickerGetter &kicker, QtTaskTree::WorkflowPolicy policy = WorkflowPolicy::StopOnError)

Creates a delaying element, taking kicker returning ExecutableItem to be run in parallel with a Do body. The Do body is delayed until the QStoredBarrier passed to the kicker is advanced. The ExecutableItem returned by the kicker and the Do body will run in parallel using the policy.

For example, if you want to delay the execution of the subsequent tasks until the QProcess is started, the recipe could look like:

     const auto kicker = [](const QStoredBarrier &barrier) {
         const auto onSetup = [barrier](QProcess &process) {
             QObject::connect(&process, &QProcess::started, barrier.activeStorage(), &QBarrier::advance);
             ... // Setup process program, arguments, environment, etc...
         };
         return QProcessTask(onSetup);
     };

     const Group recipe {
         When (kicker) >> Do {
             delayedTask1,
             ...
         }
     };

When the above recipe is executed, the QTaskTree runs a QProcessTask in parallel with a Do body. A Do body is initially on hold - it will continue after the barrier passed to the kicker will advance. This will happen as soon as the QProcess is started. Since than, QProcess runs in parallel with the Do body.

See also Do.

### `[explicit]` template < typename Task, typename Adapter, typename Deleter, typename Signal > When::When(const QtTaskTree::QCustomTask<Task, Adapter, Deleter> &customTask, Signal signal, QtTaskTree::WorkflowPolicy policy = QtTaskTree::WorkflowPolicy::StopOnError)

Creates a delaying element, taking customTask and its `Task`'s signal to be run in parallel with a Do body. The Do body is delayed until the `Task`'s signal is emitted. The customTask and the Do body will run in parallel using the policy.

The code from the other When's constructor could be simplified to:

     const auto onSetup = [barrier](QProcess &process) {
         ... // Setup process program, arguments, environment, etc...
     };

     const Group recipe {
         When (QProcessTask(onSetup), &QProcess::started) >> Do {
             delayedTask1,
             ...
         }
     };

Note: The `Task` type of the passed customTask needs to be derived from QObject.

See also Do.

## Related Non-Members

### QtTaskTree::Group operator>>(const QtTaskTree::When &whenItem, const QtTaskTree::Do &doItem)

Combines whenItem with doItem body and returns a Group ready to be used in task tree recipes.