QJSValue Class

The QJSValue class acts as a container for Qt/JavaScript data types. More...

Header: #include <QJSValue>
qmake: QT += qml
Since: Qt 5.0

This class was introduced in Qt 5.0.

Public Types

enum ErrorType { GenericError, RangeError, ReferenceError, SyntaxError, TypeError, URIError }
enum SpecialValue { UndefinedValue, NullValue }
typedef QJSValueList

Detailed Description

QJSValue supports the types defined in the ECMA-262 standard: The primitive types, which are Undefined, Null, Boolean, Number, and String; and the Object and Array types. Additionally, built-in support is provided for Qt/C++ types such as QVariant and QObject.

For the object-based types (including Date and RegExp), use the newT() functions in QJSEngine (e.g. QJSEngine::newObject()) to create a QJSValue of the desired type. For the primitive types, use one of the QJSValue constructor overloads. For other types, e.g. registered gadget types such as QPoint, you can use QJSEngine::toScriptValue.

The methods named isT() (e.g. isBool(), isUndefined()) can be used to test if a value is of a certain type. The methods named toT() (e.g. toBool(), toString()) can be used to convert a QJSValue to another type. You can also use the generic qjsvalue_cast() function.

Object values have zero or more properties which are themselves QJSValues. Use setProperty() to set a property of an object, and call property() to retrieve the value of a property.

 QJSEngine myEngine;
 QJSValue myObject = myEngine.newObject();
 QJSValue myOtherObject = myEngine.newObject();
 myObject.setProperty("myChild", myOtherObject);
 myObject.setProperty("name", "John Doe");

If you want to iterate over the properties of a script object, use the QJSValueIterator class.

Object values have an internal prototype property, which can be accessed with prototype() and setPrototype().

Function objects (objects for which isCallable()) returns true) can be invoked by calling call(). Constructor functions can be used to construct new objects by calling callAsConstructor().

Use equals() or strictlyEquals() to compare a QJSValue to another.

Note that a QJSValue for which isObject() is true only carries a reference to an actual object; copying the QJSValue will only copy the object reference, not the object itself. If you want to clone an object (i.e. copy an object's properties to another object), you can do so with the help of a for-in statement in script code, or QJSValueIterator in C++.

Working With Arrays

To create an array using QJSValue, use QJSEngine::newArray():

 // Assumes that this class was declared in QML.
 QJSValue jsArray = engine->newArray(3);

To set individual elements in the array, use the setProperty(quint32 arrayIndex, const QJSValue &value) overload. For example, to fill the array above with integers:

 for (int i = 0; i < 3; ++i) {
     jsArray.setProperty(i, QRandomGenerator::global().generate());
 }

To determine the length of the array, access the "length" property. To access array elements, use the property(quint32 arrayIndex) overload. The following code reads the array we created above back into a list:

 QVector<int> integers;
 const int length = jsArray.property("length").toInt();
 for (int i = 0; i < length; ++i) {
     integers.append(jsArray.property(i).toInt());
 }

See also QJSEngine and QJSValueIterator.

Member Type Documentation

enum QJSValue::ErrorType

Use this enum for JavaScript language-specific types of Error objects.

They may be useful when emulating language features in C++ requires the use of specialized exception types. In addition, they may help to more clearly communicate certain typical conditions, instead of throwing a generic JavaScript exception. For example, code that deals with networking and resource locators may find it useful to propagate errors related to malformed locators using the URIError type.

ConstantValueDescription
QJSValue::GenericError1A generic Error object, but not of a specific sub-type.
QJSValue::RangeError3A value did not match the expected set or range.
QJSValue::ReferenceError4A non-existing variable referenced.
QJSValue::SyntaxError5An invalid token or sequence of tokens was encountered that does not conform with the syntax of the language.
QJSValue::TypeError6An operand or argument is incompatible with the type expected.
QJSValue::URIError7A URI handling function was used incorrectly or the URI provided is malformed.

This enum was introduced or modified in Qt 5.12.

enum QJSValue::SpecialValue

This enum is used to specify a single-valued type.

ConstantValueDescription
QJSValue::UndefinedValue1An undefined value.
QJSValue::NullValue0A null value.

Related Non-Members

typedef QJSValueList

This is a typedef for a QList<QJSValue>.