- Extending Qt Creator Manual
- TaskTree
- If
- Extending Qt Creator Manual 19.0.2

# QtTaskTree::If Class

class QtTaskTree::If

An "if" element used in conditional expressions. More...

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

- List of all members, including inherited members

Note: All functions in this class are reentrant.

## Public Functions

| If(const QtTaskTree::ExecutableItem &condition)
| If(Handler &&handler)

## Detailed Description

An initial condition element of the conditional expressions. Must always be followed by Then element.

The conditional expressions in the TaskTree module enable processing of branch conditions and their bodies in an asynchronous way. For example:

     const Group recipe {
         If (conditionTask1) >> Then {
             bodyTask1
         } >> ElseIf (conditionTask2) >> Then {
             bodyTask2
         } >> Else {
             bodyTask3
         }
     };

When the above recipe is run with QTaskTree, the task tree starts the asynchronous `conditionTask1` first. After it's finished, depending on the result, the task tree may either execute the `bodyTask1` when the `conditionTask1` finished with success, or dispatch another condition by executing `conditionTask2` otherwise. In case the `bodyTask1` is executed, no other tasks are executed, and the result of `bodyTask1` is the final result of the whole conditional expression.

See also Then, Else, and ElseIf.

## Member Function Documentation

### `[explicit]` If::If(const QtTaskTree::ExecutableItem &condition)

Creates an initial condition element with condition task to be used in conditional expression. The running QTaskTree executes the passed condition first, and after it's finished, either the Then branch is executed (on success), or optionally provided Else branch or ElseIf condition otherwise.

The passed condition may consist of multiple tasks enclosed in Group element, or be a conjunction or disjunction of them, like:

     const Group subRecipe {
         parallel,
         parallelConditionTask1,
         parallelConditionTask2
     };

     const Group recipe {
         If (conditionTask1 && !conditionTask2) >> Then {
             bodyTask1
         } >> ElseIf (subRecipe) >> Then {
             bodyTask2
         } >> Else {
             bodyTask3
         }
     };

### `[explicit]` template <typename Handler, std::enable_if_t<!std::is_base_of_v<ExecutableItem, std::decay_t<Handler>>, bool> = true> If::If(Handler &&handler)

A helper constructor accepting the synchronous handler to be executed when evaluating the initial condition by the running QTaskTree.

It's a shortcut for:

     If (QSyncTask(handler))

See QSyncTask for more information on what handler types are acceptable.

This is an overloaded function.

See also QSyncTask and ElseIf.