QtTaskTree::Iterator Class | Qt TaskTree | Qt 6.11.1
Description: Base class to be used as an iterator inside For element.

- Qt 6.11
- Qt TaskTree
- C++ Classes
- Iterator
- Qt 6.11.1 Reference Documentation

# QtTaskTree::Iterator Class

class QtTaskTree::Iterator

Base class to be used as an iterator inside For element. 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
Inherited By: | QtTaskTree::ForeverIterator, QtTaskTree::ListIterator, QtTaskTree::RepeatIterator, and QtTaskTree::UntilIterator

- List of all members, including inherited members

Note: All functions in this class are reentrant.

## Public Functions

qsizetype | iteration() const

## Detailed Description

See also For, ForeverIterator, RepeatIterator, UntilIterator, and ListIterator.

## Member Function Documentation

### qsizetype Iterator::iteration() const

Returns the iteration index of the currently executing handler inside a Do body of the For (Iterator) >> construct. Use this function only from inside the handler body of any GroupItem element placed in the Do body of the recipe, otherwise you may expect a crash. Make sure that Iterator is passed to the For element.

Example usage:

     const QList<std::chrono::seconds> timeouts = { 5s, 1s, 3s };
     const ListIterator iterator(timeouts);

     const auto onSetup = [iterator](std::chrono::milliseconds &timeout) {
         timeout = *iterator;
         qDebug() << "Starting" << iterator.iteration() << "iteration with timeout"
                  << *iterator << "seconds.";
     };
     const auto onDone = [iterator] {
         qDebug() << "Finished" << iterator.iteration() << "iteration with timeout"
                  << *iterator << "seconds.";
     };

     const Group sequentialRecipe = For(iterator) >> Do {
         QTimeoutTask(onSetup, onDone)
     };

     const Group parallelRecipe = For(iterator) >> Do {
         parallel,
         QTimeoutTask(onSetup, onDone)
     };

The output when executing `sequentialRecipe` will be:

     Starting 0 iteration with timeout 5s seconds.
     Finished 0 iteration with timeout 5s seconds.
     Starting 1 iteration with timeout 1s seconds.
     Finished 1 iteration with timeout 1s seconds.
     Starting 2 iteration with timeout 3s seconds.
     Finished 2 iteration with timeout 3s seconds.

In sequential mode the order of iteration indices in done handlers is guaranteed to be preserved.

The output when executing `parallelRecipe` will be:

     Starting 0 iteration with timeout 5s seconds.
     Starting 1 iteration with timeout 1s seconds.
     Starting 2 iteration with timeout 3s seconds.
     Finished 1 iteration with timeout 1s seconds.
     Finished 2 iteration with timeout 3s seconds.
     Finished 0 iteration with timeout 5s seconds.

In parallel mode the order of iteration indices in done handlers isn't guaranteed to be preserved, and depends on the order of finished tasks. The returned iteration index inside the parallel Do body's done handler matches the index of the original iteration for the corresponding setup handler, so the order of iteration indices in subsequent done handlers may not be ascending.