本页内容

QtTaskTree::RepeatIterator Class

class QtTaskTree::RepeatIterator

要在 For 元素内部使用的迭代器。更多内容...

头文件: #include <qtasktree.h>
CMake: find_package(Qt6 REQUIRED COMPONENTS TaskTree)
target_link_libraries(mytarget PRIVATE Qt6::TaskTree)
qmake: QT += tasktree
自: Qt 6.11
继承自: QtTaskTree::Iterator
状态: 技术预览

该类处于技术预览阶段,内容可能会有变动。

注意:该类中的所有函数均为可重入函数。

公共函数

RepeatIterator(qsizetype count)

详细说明

另请参阅 Iterator 、ForeverIterator 、UntilIterator 和ListIterator 。

成员函数文档

[explicit] RepeatIterator::RepeatIterator(qsizetype count)

为 For (RepeatIterator(count)) >> Do {} 结构构建重复迭代器。该迭代器将重复执行count 次。

使用示例:

static const intmaxTries= 5;
static const intmaxNumber= 10;
const intluckyNumber=QRandomGenerator().global()->bounded(maxNumber);

qDebug() << "Today's lucky number is:" << luckyNumber;
qDebug() << "You have" << maxTries << "lottery tickets.";
qDebug() << "Let's start the drawing...";

const autoonNumberCheck= [luckyNumber]{
    const intdrawnNumber=QRandomGenerator().global()->bounded(maxNumber);
    qDebug() << "You have drawn:" << drawnNumber;
   returndrawnNumber==luckyNumber;
};

const autoonDone= [](DoneWith result) {
    if(result==DoneWith::Success)
        qDebug() << "You have won! Congratulations!";
   else if(result==DoneWith::Error)
        qDebug() << "You have lost. Try again.";
};

constGroup recipe {
    For (RepeatIterator(maxTries))>>Do {
        stopOnSuccess,
        timeoutTask(1s),
        QSyncTask(onNumberCheck)
    },
    onGroupDone(onDone)
};

当 `recipe ` 由 `QTaskTree` 启动并成功完成时,可能的输出如下:

Today's lucky number is: 6
You have 5 lottery tickets.
Let's start the drawing...
You have drawn: 9
You have drawn: 6
You have won! Congratulations!

如果执行时发生错误,输出可能如下所示:

Today's lucky number is: 8
You have 5 lottery tickets.
Let's start the drawing...
You have drawn: 1
You have drawn: 7
You have drawn: 6
You have drawn: 7
You have drawn: 2
You have lost. Try again.

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