QJSValue Class▲
-
Header: QJSValue
-
CMake:
find_package(Qt6 REQUIRED COMPONENTS Qml)
target_link_libraries(mytarget PRIVATE Qt6::Qml)
-
qmake: QT += qml
-
Group: QJSValue is part of qtjavascript
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