QtTaskTree::When Class

class QtTaskTree::When

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

Header: #include <qbarriertask.h>

Note: All functions in this class are reentrant.

Public Functions

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

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

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