本页内容

QJSValueIterator Class

QJSValueIterator 类为QJSValue 提供了一个 Java 风格的迭代器。更多内容...

头文件: #include <QJSValueIterator>
CMake: find_package(Qt6 REQUIRED COMPONENTS Qml)
target_link_libraries(mytarget PRIVATE Qt6::Qml)
qmake: QT += qml

公共函数

QJSValueIterator(const QJSValue &object)
~QJSValueIterator()
bool hasNext() const
QString name() const
bool next()
QJSValue value() const
QJSValueIterator &operator=(QJSValue &object)

详细说明

QJSValueIterator 的构造函数接受一个QJSValue 作为参数。构造完成后,迭代器位于属性序列的最前端。以下是如何遍历QJSValue 的所有属性的方法:

QJSValue object;
...
QJSValueIterator it(object);
while(it.hasNext()) {
    it.next();
    qDebug() << it.name() << ": " << it.value().toString();
}

next() 用于向前移动迭代器。name() 和value() 函数分别返回被跳过的最后一个项目的名称和值。

请注意,QJSValueIterator 仅遍历QJSValue 自身的属性;也就是说,它不会沿着原型链遍历。您可以使用如下循环来沿着原型链遍历:

QJSValue obj=...;// 要迭代的对象
while(obj.isObject()) {
    QJSValueIterator it(obj);
    while(it.hasNext()) {
        it.next();
        qDebug() << it.name();
    }
    obj=obj.prototype();
}

另请参阅 QJSValue::property()。

成员函数文档

QJSValueIterator::QJSValueIterator(const QJSValue &object)

构建一个用于遍历object 的迭代器。该迭代器被设置为位于属性序列的开头(即第一个属性之前)。

[noexcept] QJSValueIterator::~QJSValueIterator()

销毁迭代器。

bool QJSValueIterator::hasNext() const

如果迭代器前方至少还有一个项目(即迭代器未位于属性序列的末尾),则返回 true;否则返回 false。

另请参阅 next()。

QString QJSValueIterator::name() const

返回使用next() 跳过的最后一个属性的名称。

另请参阅 value()。

bool QJSValueIterator::next()

将迭代器向前移动一个位置。如果迭代器前方至少还有一个项目(即迭代器尚未到达属性序列的末尾),则返回 true;否则返回 false。

另请参阅 hasNext() 和name()。

QJSValue QJSValueIterator::value() const

返回使用next() 跳过的最后一个属性的值。

另请参阅 name()。

QJSValueIterator &QJSValueIterator::operator=(QJSValue &object)

使迭代器在object 上运行。迭代器被设置为位于属性序列的开头(在第一个属性之前)。

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