6.0.0-rc4Source

QJsonValueRef Class

The QJsonValueRef class is a helper class for QJsonArray and QJsonObject. More…

Header#include <QJsonValueRef>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes
InheritsQJsonValueConstRef

List of all members, including inherited members

Public Functions

QJsonValueRef(QJsonArray *array, qsizetype idx)
QJsonValueRef(QJsonObject *object, qsizetype idx)
QJsonValueRef(const QJsonValueRef &)
QJsonValueRef &operator=(const QJsonValue &val)
QJsonValueRef &operator=(const QJsonValueRef &val)

Detailed Description

When you get an object of type QJsonValueRef, for example from the array's operator[] or by dereferencing a non-const iterator, you can use it as if it were a reference to a QJsonValue. Assigning to it replaces the element in the container it came from.

QJsonArray array = {1, 2, 3};
array[1] = "two";
for (QJsonValueRef v : array) {
    if (v.isDouble())
        v = v.toInt() * 2;
}

See also QJsonValueConstRef and QJsonValue.

Differences from Qt

A reference stores the container and an index, so it reads whatever is at that index now. Changing the container's size while holding a reference can make it refer to another element or to none, which then reads as undefined; assigning through a reference to a missing element does nothing. Qt asserts in these cases.

Member Function Documentation

QJsonValueRef::QJsonValueRef(QJsonArray *array, qsizetype idx)

Creates a reference to the element at index idx of array.

QJsonValueRef::QJsonValueRef(QJsonObject *object, qsizetype idx)

Creates a reference to the entry at position idx of object, in the sorted order of its keys.

QJsonValueRef::QJsonValueRef(const QJsonValueRef &)

Creates a copy of the reference; both refer to the same element.

QJsonValueRef & QJsonValueRef::operator=(const QJsonValue &val)

Replaces the element with val and returns a reference to it. Nothing happens if the element does not exist.

QJsonValueRef & QJsonValueRef::operator=(const QJsonValueRef &val)

This is an overloaded function.

Replaces the element with the value val refers to. This assigns the value, not the reference.