QScriptValue Class

The QScriptValue class acts as a container for the Qt Script data types. More...

Header: #include <QScriptValue>
qmake: QT += script
Since: Qt 4.3

This class was introduced in Qt 4.3.

Public Types

enum PropertyFlag { ReadOnly, Undeletable, SkipInEnumeration, PropertyGetter, PropertySetter, KeepExistingFlags }
enum ResolveFlag { ResolveLocal, ResolvePrototype }
enum SpecialValue { UndefinedValue, NullValue }

Detailed Description

QScriptValue supports the types defined in the ECMA-262 standard: The primitive types, which are Undefined, Null, Boolean, Number, and String; and the Object type. Additionally, Qt Script has built-in support for QVariant, QObject and QMetaObject.

For the object-based types (including Date and RegExp), use the newT() functions in QScriptEngine (e.g. QScriptEngine::newObject()) to create a QScriptValue of the desired type. For the primitive types, use one of the QScriptValue constructor overloads.

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 QScriptValue to another type. You can also use the generic qscriptvalue_cast() function.

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

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

Each property can have a set of attributes; these are specified as the third (optional) argument to setProperty(). The attributes of a property can be queried by calling the propertyFlags() function. The following code snippet creates a property that cannot be modified by script code:

 QScriptValue val(&myEngine, 123);
 myObject.setProperty("myReadOnlyProperty", val, QScriptValue::ReadOnly);

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

Object values have an internal prototype property, which can be accessed with prototype() and setPrototype(). Properties added to a prototype are shared by all objects having that prototype; this is referred to as prototype-based inheritance. In practice, it means that (by default) the property() function will automatically attempt to look up look the property in the prototype() (and in the prototype of the prototype(), and so on), if the object itself does not have the requested property. Note that this prototype-based lookup is not performed by setProperty(); setProperty() will always create the property in the script object itself. For more information, see the Qt Script documentation.

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

Use equals(), strictlyEquals() and lessThan() to compare a QScriptValue to another.

Object values can have custom data associated with them; see the setData() and data() functions. By default, this data is not accessible to scripts; it can be used to store any data you want to associate with the script object. Typically this is used by custom class objects (see QScriptClass) to store a C++ type that contains the "native" object data.

Note that a QScriptValue for which isObject() is true only carries a reference to an actual object; copying the QScriptValue 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 QScriptValueIterator in C++.

See also QScriptEngine and QScriptValueIterator.

Member Type Documentation

enum QScriptValue::PropertyFlag

This enum describes the attributes of a property.

ConstantValueDescription
QScriptValue::ReadOnly0x00000001The property is read-only. Attempts by Qt Script code to write to the property will be ignored.
QScriptValue::Undeletable0x00000002Attempts by Qt Script code to delete the property will be ignored.
QScriptValue::SkipInEnumeration0x00000004The property is not to be enumerated by a for-in enumeration.
QScriptValue::PropertyGetter0x00000008The property is defined by a function which will be called to get the property value.
QScriptValue::PropertySetter0x00000010The property is defined by a function which will be called to set the property value.

This flag is used to indicate that an existing property is a QObject member (a property or method).

ConstantValueDescription
QScriptValue::KeepExistingFlags0x00000800This value is used to indicate to setProperty() that the property's flags should be left unchanged. If the property doesn't exist, the default flags (0) will be used.

Flags in this range are not used by Qt Script, and can be used for custom purposes.

enum QScriptValue::ResolveFlag

This enum specifies how to look up a property of an object.

ConstantValueDescription
QScriptValue::ResolveLocal0x00Only check the object's own properties.
QScriptValue::ResolvePrototype0x01Check the object's own properties first, then search the prototype chain. This is the default.

Check the object's own properties first, then search the scope chain.

Check the object's own properties first, then search the prototype chain, and finally search the scope chain.

enum QScriptValue::SpecialValue

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

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