本页内容

QJsonObject::iterator Class

class QJsonObject::iterator

QJsonObject::iterator 类为QJsonObject 提供了一个STL风格的非const迭代器。更多内容...

QJsonObject::iterator 的比较

类别可比较类型
strongQJsonObject::iterator
strongQJsonObject::const_iterator.

公共类型

公共函数

iterator()
iterator(const QJsonObject::iterator &other)
QString key() const
(since 6.10) QAnyStringView keyView() const
QJsonValueRef value() const
QJsonValueRef operator*() const
QJsonObject::iterator operator+(qsizetype j) const
QJsonObject::iterator &operator++()
QJsonObject::iterator operator++(int)
QJsonObject::iterator &operator+=(qsizetype j)
qsizetype operator-(QJsonObject::iterator other) const
QJsonObject::iterator operator-(qsizetype j) const
QJsonObject::iterator &operator--()
QJsonObject::iterator operator--(int)
QJsonObject::iterator &operator-=(qsizetype j)
QJsonValueRef *operator->()
const QJsonValueConstRef *operator->() const
QJsonValueRef operator[](qsizetype j) const
bool operator!=(const QJsonObject::iterator &lhs, const QJsonObject::const_iterator &rhs)
bool operator!=(const QJsonObject::iterator &lhs, const QJsonObject::iterator &rhs)
bool operator<(const QJsonObject::iterator &lhs, const QJsonObject::const_iterator &rhs)
bool operator<(const QJsonObject::iterator &lhs, const QJsonObject::iterator &rhs)
bool operator<=(const QJsonObject::iterator &lhs, const QJsonObject::const_iterator &rhs)
bool operator<=(const QJsonObject::iterator &lhs, const QJsonObject::iterator &rhs)
bool operator==(const QJsonObject::iterator &lhs, const QJsonObject::const_iterator &rhs)
bool operator==(const QJsonObject::iterator &lhs, const QJsonObject::iterator &rhs)
bool operator>(const QJsonObject::iterator &lhs, const QJsonObject::const_iterator &rhs)
bool operator>(const QJsonObject::iterator &lhs, const QJsonObject::iterator &rhs)
bool operator>=(const QJsonObject::iterator &lhs, const QJsonObject::const_iterator &rhs)
bool operator>=(const QJsonObject::iterator &lhs, const QJsonObject::iterator &rhs)

详细说明

QJsonObject::iterator 允许您遍历QJsonObject ,并修改存储在特定键下的值(但不能修改键)。 如果您想遍历一个 constQJsonObject ,应使用QJsonObject::const_iterator 。通常,对非 constQJsonObject 也使用QJsonObject::const_iterator 是一种良好的编程习惯,除非您需要通过迭代器更改QJsonObject 。常量迭代器速度稍快,且能提高代码的可读性。

默认的QJsonObject::iterator 构造函数会创建一个未初始化的迭代器。在开始迭代之前,必须使用QJsonObject 函数(如QJsonObject::begin()、QJsonObject::end() 或QJsonObject::find())对其进行初始化。

同一个对象上可以使用多个迭代器。但是,一旦对象被修改,现有的迭代器将变为悬空迭代器。

另请参阅 QJsonObject::const_iterator 、Qt XML 中 JSON 支持以及游戏的保存与加载。

成员类型文档

iterator::iterator_category

std::random_access_iterator_tag的同义词,表示该迭代器是一个随机访问迭代器。

注意:在 Qt 5.6 之前的版本中,此值曾被错误地设置为std::bidirectional_iterator_tag。

成员函数文档

iterator::iterator()

构建一个未初始化的迭代器。

不得对未初始化的迭代器调用诸如key()、value() 和 operator++() 之类的函数。在使用该迭代器之前,请使用 operator=() 向其赋值。

另请参阅 QJsonObject::begin() 和QJsonObject::end()。

[constexpr noexcept default] iterator::iterator(const QJsonObject::iterator &other)

复制并构造一个iterator 实例。

QString iterator::key() const

返回当前项的键。

虽然无法通过迭代器直接更改项的键,但可以通过先调用 `QJsonObject::erase()`,然后调用 `QJsonObject::insert()` 来实现。

另请参阅 value() 和keyView()。

[since 6.10] QAnyStringView iterator::keyView() const

返回当前项的键,格式为QAnyStringView 。此函数不会分配内存。

由于QJsonObject 将键以US-ASCII、UTF-8或UTF-16格式存储,因此返回的QAnyStringView 可能采用上述任一编码格式。

虽然无法通过迭代器直接更改项的键,但可以通过依次调用QJsonObject::erase()和QJsonObject::insert()来实现。

该函数在 Qt 6.10 中引入。

另请参阅 key() 和value()。

QJsonValueRef iterator::value() const

返回指向当前项值的可修改引用。

您可以在赋值语句的左侧使用 value() 方法来更改项目的值。

返回值的类型为QJsonValueRef ,这是QJsonArray 和QJsonObject 的辅助类。当您获取一个类型为QJsonValueRef 的对象时,可以将其视为对QJsonValue 的引用。如果您对其进行赋值,该赋值将应用于您从中获取该引用的QJsonArray 或QJsonObject 中的元素。

另请参阅 key()、keyView() 和operator*()。

QJsonValueRef iterator::operator*() const

返回指向当前项值的可修改引用。

与 `value()` 相同。

返回值的类型为QJsonValueRef ,这是QJsonArray 和QJsonObject 的辅助类。当您获取一个QJsonValueRef 类型的对象时,可以将其视为对QJsonValue 的引用。如果您对其进行赋值,该赋值操作将作用于您从中获取该引用的QJsonArray 或QJsonObject 中的元素。

另请参阅 key() 和keyView()。

QJsonObject::iterator iterator::operator+(qsizetype j) const

返回一个迭代器,该迭代器指向距离当前迭代器向前j 个位置处的项。如果j 为负数,则迭代器向后移动。

另请参阅 operator-()。

QJsonObject::iterator &iterator::operator++()

前缀运算符++ (++i )将迭代器向前移动到对象中的下一个项,并返回指向新当前项的迭代器。

在 `QJsonObject::end()` 上调用此函数将导致未定义的结果。

另请参阅 operator--()。

QJsonObject::iterator iterator::operator++(int)

后缀运算符++ (i++ )将迭代器向前移动到对象中的下一个元素,并返回指向前一个当前元素的迭代器。

这是一个重载函数。

QJsonObject::iterator &iterator::operator+=(qsizetype j)

将迭代器向前移动j 个项目。如果j 为负数,则迭代器向后移动。

另请参阅 operator-=() 和operator+()。

qsizetype iterator::operator-(QJsonObject::iterator other) const

返回从other 所指向的项目到该迭代器所指向的项目之间的项目数量。

QJsonObject::iterator iterator::operator-(qsizetype j) const

返回一个迭代器,该迭代器指向从当前迭代器向后移动j 个位置处的项。如果j 为负数,则迭代器向前移动。

另请参阅 operator+()。

QJsonObject::iterator &iterator::operator--()

前缀-- 运算符(--i )会将前一个元素设为当前元素,并返回一个指向新当前元素的迭代器。

在 `QJsonObject::begin()` 上调用此函数会导致未定义的结果。

另请参阅 operator++()。

QJsonObject::iterator iterator::operator--(int)

后缀运算符-- (i-- )将前一个元素设为当前元素,并返回一个指向先前当前元素的迭代器。

这是一个重载函数。

QJsonObject::iterator &iterator::operator-=(qsizetype j)

使迭代器向后移动j 个项目。如果j 为负数,则迭代器向前移动。

另请参阅 operator+=() 和operator-()。

QJsonValueRef *iterator::operator->()

返回指向当前项目的一个可修改引用指针。

const QJsonValueConstRef *iterator::operator->() const

返回指向当前项目常量引用的一条指针。

QJsonValueRef iterator::operator[](qsizetype j) const

返回一个可修改的引用,该引用指向由该迭代器所指向的项目(位于位置*this + j 的项目)在偏移量j 处的位置。

提供此函数是为了使QJsonObject 迭代器像 C++ 指针一样工作。

返回值的类型为QJsonValueRef ,这是QJsonArray 和QJsonObject 的辅助类。当您获得一个QJsonValueRef 类型的对象时,可以将其视为对QJsonValue 的引用来使用。如果对其进行赋值,该赋值将作用于您从中获取该引用的QJsonArray 或QJsonObject 中的元素。

另请参阅 operator+()。

相关的非成员

[noexcept] bool operator!=(const QJsonObject::iterator &lhs, const QJsonObject::const_iterator &rhs)

[noexcept] bool operator!=(const QJsonObject::iterator &lhs, const QJsonObject::iterator &rhs)

如果lhs 指向的项与rhs 迭代器指向的项不同,则返回true ;否则返回false 。

另请参阅 operator==()。

[noexcept] bool operator<(const QJsonObject::iterator &lhs, const QJsonObject::const_iterator &rhs)

[noexcept] bool operator<(const QJsonObject::iterator &lhs, const QJsonObject::iterator &rhs)

如果由迭代器 `lhs ` 指向的项小于由迭代器 `rhs ` 指向的项,则返回 `true `。

[noexcept] bool operator<=(const QJsonObject::iterator &lhs, const QJsonObject::const_iterator &rhs)

[noexcept] bool operator<=(const QJsonObject::iterator &lhs, const QJsonObject::iterator &rhs)

如果由迭代器 `lhs ` 指向的项小于或等于由迭代器 `rhs ` 指向的项,则返回 `true `。

[noexcept] bool operator==(const QJsonObject::iterator &lhs, const QJsonObject::const_iterator &rhs)

[noexcept] bool operator==(const QJsonObject::iterator &lhs, const QJsonObject::iterator &rhs)

如果 `lhs ` 指向与迭代器 `rhs ` 相同的项,则返回 `true `;否则返回 `false`。

另请参阅 operator!=()。

[noexcept] bool operator>(const QJsonObject::iterator &lhs, const QJsonObject::const_iterator &rhs)

[noexcept] bool operator>(const QJsonObject::iterator &lhs, const QJsonObject::iterator &rhs)

如果由迭代器 `lhs ` 指向的项大于由迭代器 `rhs ` 指向的项,则返回 `true `。

[noexcept] bool operator>=(const QJsonObject::iterator &lhs, const QJsonObject::const_iterator &rhs)

[noexcept] bool operator>=(const QJsonObject::iterator &lhs, const QJsonObject::iterator &rhs)

如果由 `lhs ` 迭代器指向的项大于或等于由 `rhs ` 迭代器指向的项,则返回 `true `。

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