QtTaskTree::Iterator Class

class QtTaskTree::Iterator

Base class to be used as an iterator inside For element. More...

Note: All functions in this class are reentrant.

Public Functions

int iteration() const

Detailed Description

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

Member Function Documentation

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

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