|  | Home · All Classes · Main Classes · Grouped Classes · Modules · Functions |

# QScriptValue Class Reference
[QtScript module]

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

     #include <QScriptValue>

This class was introduced in Qt 4.3.

- List of all members, including inherited members

### Public Types

- enum PropertyFlag { ReadOnly, Undeletable, SkipInEnumeration, PropertyGetter, PropertySetter, UserRange }
- flags PropertyFlags
- enum ResolveFlag { ResolveLocal, ResolvePrototype, ResolveScope, ResolveFull }
- flags ResolveFlags
- enum SpecialValue { NullValue, UndefinedValue }
- enum TypeHint { NoTypeHint, NumberTypeHint, StringTypeHint }

### Public Functions

- QScriptValue ()
- QScriptValue ( const QScriptValue & other )
- QScriptValue ( QScriptEngine * engine, SpecialValue value )
- QScriptValue ( QScriptEngine * engine, bool val )
- QScriptValue ( QScriptEngine * engine, int val )
- QScriptValue ( QScriptEngine * engine, uint val )
- QScriptValue ( QScriptEngine * engine, qlonglong val )
- QScriptValue ( QScriptEngine * engine, qulonglong val )
- QScriptValue ( QScriptEngine * engine, qsreal val )
- QScriptValue ( QScriptEngine * engine, const QString & val )
- QT_ASCII_CAST_WARN_CONSTRUCTOR QScriptValue ( QScriptEngine * engine, const char * value )
- ~QScriptValue ()
- QScriptValue call ( const QScriptValue & thisObject = QScriptValue(), const QScriptValueList & args = QScriptValueList() )
- QScriptValue call ( const QScriptValue & thisObject, const QScriptValue & arguments )
- QScriptValue construct ( const QScriptValueList & args = QScriptValueList() )
- QScriptValue construct ( const QScriptValue & arguments )
- QScriptEngine * engine () const
- bool equalTo ( const QScriptValue & other ) const
- bool instanceOf ( const QScriptValue & ctorValue ) const
- bool isArray () const
- bool isBoolean () const
- bool isDate () const
- bool isError () const
- bool isFunction () const
- bool isNull () const
- bool isNumber () const
- bool isObject () const
- bool isQMetaObject () const
- bool isQObject () const
- bool isRegExp () const
- bool isString () const
- bool isUndefined () const
- bool isValid () const
- bool isVariant () const
- bool lessThan ( const QScriptValue & other ) const
- QScriptValue property ( const QString & name, const ResolveFlags & mode = ResolvePrototype ) const
- QScriptValue property ( quint32 arrayIndex, const ResolveFlags & mode = ResolvePrototype ) const
- QScriptValue prototype () const
- void setProperty ( const QString & name, const QScriptValue & value, const PropertyFlags & flags = 0 )
- void setProperty ( quint32 arrayIndex, const QScriptValue & value, const PropertyFlags & flags = 0 )
- void setPrototype ( const QScriptValue & prototype )
- void setVariantValue ( const QVariant & value )
- bool strictEqualTo ( const QScriptValue & other ) const
- bool toBoolean () const
- QDateTime toDateTime () const
- qint32 toInt32 () const
- qsreal toInteger () const
- qsreal toNumber () const
- QScriptValue toObject () const
- QScriptValue toPrimitive ( TypeHint hint = NoTypeHint ) const
- const QMetaObject * toQMetaObject () const
- QObject * toQObject () const
- QRegExp toRegExp () const
- QString toString () const
- quint16 toUInt16 () const
- quint32 toUInt32 () const
- QVariant toVariant () const
- QScriptValue & operator= ( const QScriptValue & other )

### Related Non-Members

- T qscriptvalue_cast ( const QScriptValue & value )

## Detailed Description

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

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 defines two types: `Variant` (a QVariant), and `QObject` (a pointer to a QObject (or subclass)). Custom types are supported by means of the Qt meta type system; see qScriptRegisterMetaType().

To obtain a QScriptValue, you use one of the scriptValue() or scriptValueFromT() methods in QScriptEngine (e.g. `QScriptEngine::scriptValue(123)`).

The methods named isT() (e.g. isBoolean(), isUndefined()) can be used to test if a value is of a certain type. The methods named toT() (e.g. toBoolean(), 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.

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. For more information, see the QtScript 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().

See also QScriptEngine.

## Member Type Documentation

### enum QScriptValue::PropertyFlag
flags QScriptValue::PropertyFlags

This enum describes the attributes of a property.

Constant | Value | Description
--- | --- | ---
`QScriptValue::ReadOnly` | `0x00000001` | The property is read-only. Attempts by Qt Script code to write to the property will be ignored.
`QScriptValue::Undeletable` | `0x00000002` | Attempts by Qt Script code to `delete` the property will be ignored.
`QScriptValue::SkipInEnumeration` | `0x00000004` | The property is not to be enumerated by a `for-in` enumeration.
`QScriptValue::PropertyGetter` | `0x00000400` | The property is defined by a function which will be called to get the property value.
`QScriptValue::PropertySetter` | `0x00000800` | The property is defined by a function which will be called to set the property value.
`QScriptValue::UserRange` | `0xffff0000` | Flags in this range are not used by Qt Script, and can be used for custom purposes.

The PropertyFlags type is a typedef for QFlags<PropertyFlag>. It stores an OR combination of PropertyFlag values.

### enum QScriptValue::ResolveFlag
flags QScriptValue::ResolveFlags

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

Constant | Value | Description
--- | --- | ---
`QScriptValue::ResolveLocal` | `0x00` | Only check the object's own properties.
`QScriptValue::ResolvePrototype` | `0x01` | Check the object's own properties first, then search the prototype chain. This is the default.
`QScriptValue::ResolveScope` | `0x02` | Check the object's own properties first, then search the scope chain.
`QScriptValue::ResolveFull` | `ResolvePrototype | ResolveScope` | Check the object's own properties first, then search the prototype chain, and finally search the scope chain.

The ResolveFlags type is a typedef for QFlags<ResolveFlag>. It stores an OR combination of ResolveFlag values.

### enum QScriptValue::SpecialValue

### enum QScriptValue::TypeHint

This enum is used to provide toPrimitive() with the desired type of the return value.

Constant | Value | Description
--- | --- | ---
`QScriptValue::NoTypeHint` | `0` | No hint.
`QScriptValue::NumberTypeHint` | `1` | A number value is desired.
`QScriptValue::StringTypeHint` | `2` | A string value is desired.

## Member Function Documentation

### QScriptValue::QScriptValue ()

Constructs an invalid QScriptValue.

### QScriptValue::QScriptValue ( const QScriptValue & other )

Constructs a new QScriptValue that is a copy of other.

### QScriptValue::QScriptValue ( QScriptEngine * engine, SpecialValue value )

### QScriptValue::QScriptValue ( QScriptEngine * engine, bool val )

Constructs a new QScriptValue with a boolean value, val.

### QScriptValue::QScriptValue ( QScriptEngine * engine, int val )

Constructs a new QScriptValue with an integer value, val.

### QScriptValue::QScriptValue ( QScriptEngine * engine, uint val )

Constructs a new QScriptValue with an unsigned integer value, val.

### QScriptValue::QScriptValue ( QScriptEngine * engine, qlonglong val )

Constructs a new QScriptValue with a qlonglong value, val.

### QScriptValue::QScriptValue ( QScriptEngine * engine, qulonglong val )

Constructs a new QScriptValue with a qulonglong value, val.

### QScriptValue::QScriptValue ( QScriptEngine * engine, qsreal val )

Constructs a new QScriptValue with a qsreal value, val.

### QScriptValue::QScriptValue ( QScriptEngine * engine, const QString & val )

Constructs a new QScriptValue with a string value, val.

### QT_ASCII_CAST_WARN_CONSTRUCTOR QScriptValue::QScriptValue ( QScriptEngine * engine, const char * value )

Constructs a new QScriptValue with a string value, val.

### QScriptValue::~QScriptValue ()

Destroys this QScriptValue.

### QScriptValue QScriptValue::call ( const QScriptValue & thisObject = QScriptValue(), const QScriptValueList & args = QScriptValueList() )

Calls this QScriptValue as a function, using thisObject as the `this' object in the function call, and passing args as arguments to the function. Returns the value returned from the function.

If this QScriptValue is not a function, call() does nothing and returns an invalid QScriptValue.

Note that if thisObject is not an object, the global object (see QScriptEngine::globalObject()) will be used as the `this' object.

See also construct().

### QScriptValue QScriptValue::call ( const QScriptValue & thisObject, const QScriptValue & arguments )

This is an overloaded member function, provided for convenience.

Calls this QScriptValue as a function, using thisObject as the `this' object in the function call, and passing arguments as arguments to the function. Returns the value returned from the function.

If this QScriptValue is not a function, call() does nothing and returns an invalid QScriptValue.

arguments can be an arguments object, an array, null or undefined; any other type will cause a TypeError to be thrown.

Note that if thisObject is not an object, the global object (see QScriptEngine::globalObject()) will be used as the `this' object.

See also construct() and QScriptContext::argumentsObject().

### QScriptValue QScriptValue::construct ( const QScriptValueList & args = QScriptValueList() )

Creates a new `Object` and calls this QScriptValue as a constructor, using the created object as the `this' object and passing args as arguments. If the return value from the constructor call is an object, then that object is returned; otherwise the created object is returned.

If this QScriptValue is not a function, construct() does nothing and returns an invalid QScriptValue.

arguments can be an arguments object, an array, null or undefined; any other type will cause a TypeError to be thrown.

See also call() and newObject().

### QScriptValue QScriptValue::construct ( const QScriptValue & arguments )

This is an overloaded member function, provided for convenience.

Creates a new `Object` and calls this QScriptValue as a constructor, using the created object as the `this' object and passing arguments as arguments. If the return value from the constructor call is an object, then that object is returned; otherwise the created object is returned.

If this QScriptValue is not a function, construct() does nothing and returns an invalid QScriptValue.

arguments can be an arguments object, an array, null or undefined. Any other type will cause a TypeError to be thrown.

See also call(), newObject(), and QScriptContext::argumentsObject().

### QScriptEngine * QScriptValue::engine () const

Returns the QScriptEngine that created this QScriptValue, or 0 if this QScriptValue is invalid.

### bool QScriptValue::equalTo ( const QScriptValue & other ) const

Returns true if this QScriptValue is equal to other, otherwise returns false. The comparison follows the behavior described in ECMA-262 section 11.9.3, "The Abstract Equality Comparison Algorithm".

### bool QScriptValue::instanceOf ( const QScriptValue & ctorValue ) const

Returns true if this QScriptValue is an instance of ctorValue; otherwise returns false.

A QScriptValue A is considered to be an instance of QScriptValue B if B is in the prototype chain of A.

### bool QScriptValue::isArray () const

Returns true if this QScriptValue is an object of the Array class; otherwise returns false.

See also QScriptEngine::newArray().

### bool QScriptValue::isBoolean () const

Returns true if this QScriptValue is of the primitive type Boolean; otherwise returns false.

See also toBoolean().

### bool QScriptValue::isDate () const

Returns true if this QScriptValue is an object of the Date class; otherwise returns false.

See also QScriptEngine::newDate().

### bool QScriptValue::isError () const

Returns true if this QScriptValue is an object of the Error class; otherwise returns false.

See also QScriptContext::throwError().

### bool QScriptValue::isFunction () const

Returns true if this QScriptValue is a function; otherwise returns false.

See also call().

### bool QScriptValue::isNull () const

Returns true if this QScriptValue is of the primitive type Null; otherwise returns false.

See also QScriptEngine::nullScriptValue().

### bool QScriptValue::isNumber () const

Returns true if this QScriptValue is of the primitive type Number; otherwise returns false.

See also toNumber().

### bool QScriptValue::isObject () const

Returns true if this QScriptValue is of the Object type; otherwise returns false.

Note that function values, variant values and QObject values are objects, so this function will return true for such values.

See also toObject(), toPrimitive(), and QScriptEngine::newObject().

### bool QScriptValue::isQMetaObject () const

Returns true if this QScriptValue is a QMetaObject; otherwise returns false.

See also toQMetaObject() and QScriptEngine::newQMetaObject().

### bool QScriptValue::isQObject () const

Returns true if this QScriptValue is a QObject; otherwise returns false.

See also toQObject() and QScriptEngine::newQObject().

### bool QScriptValue::isRegExp () const

Returns true if this QScriptValue is an object of the RegExp class; otherwise returns false.

See also QScriptEngine::newRegExp().

### bool QScriptValue::isString () const

Returns true if this QScriptValue is of the primitive type String; otherwise returns false.

See also toString().

### bool QScriptValue::isUndefined () const

Returns true if this QScriptValue is of the primitive type Undefined; otherwise returns false.

See also QScriptEngine::undefinedScriptValue().

### bool QScriptValue::isValid () const

Returns true if this QScriptValue is valid; otherwise returns false.

### bool QScriptValue::isVariant () const

Returns true if this QScriptValue is a variant value; otherwise returns false.

See also toVariant() and QScriptEngine::newVariant().

### bool QScriptValue::lessThan ( const QScriptValue & other ) const

Returns true if this QScriptValue is less than other, otherwise returns false. The comparison follows the behavior described in ECMA-262 section 11.8.5, "The Abstract Relational Comparison Algorithm".

### QScriptValue QScriptValue::property ( const QString & name, const ResolveFlags & mode = ResolvePrototype ) const

Returns the value of this QScriptValue's property with the given name, using the given mode to resolve the property.

If no such property exists, an invalid QScriptValue is returned.

See also setProperty().

### QScriptValue QScriptValue::property ( quint32 arrayIndex, const ResolveFlags & mode = ResolvePrototype ) const

This is an overloaded member function, provided for convenience.

Returns the property at the given arrayIndex.

This function is provided for convenience and performance when working with array objects.

### QScriptValue QScriptValue::prototype () const

If this QScriptValue is an object, returns the internal prototype (`__proto__` property) of this object; otherwise returns an invalid QScriptValue.

See also setPrototype() and isObject().

### void QScriptValue::setProperty ( const QString & name, const QScriptValue & value, const PropertyFlags & flags = 0 )

Sets the value of this QScriptValue's property with the given name to the given value.

If this QScriptValue is not an object, this function does nothing.

If this QScriptValue does not already have a property with name name, a new property is created; the given flags then specify how this property may be accessed by script code.

If value is invalid, the property is removed.

See also property().

### void QScriptValue::setProperty ( quint32 arrayIndex, const QScriptValue & value, const PropertyFlags & flags = 0 )

This is an overloaded member function, provided for convenience.

Sets the property at the given arrayIndex to the given value.

This function is provided for convenience and performance when working with array objects.

### void QScriptValue::setPrototype ( const QScriptValue & prototype )

If this QScriptValue is an object, sets the internal prototype (`__proto__` property) of this object to be prototype; otherwise does nothing.

See also prototype() and isObject().

### void QScriptValue::setVariantValue ( const QVariant & value )

Sets the variant value of this QScriptValue to be the given value. If this QScriptValue is not a variant, this function does nothing.

See also isVariant() and toVariant().

### bool QScriptValue::strictEqualTo ( const QScriptValue & other ) const

Returns true if this QScriptValue is equal to other using strict comparison (no conversion), otherwise returns false. The comparison follows the behavior described in ECMA-262 section 11.9.6, "The Strict Equality Comparison Algorithm".

### bool QScriptValue::toBoolean () const

Returns the boolean value of this QScriptValue, using the conversion rules described in ECMA-262 section 9.2, "ToBoolean".

See also isBoolean().

### QDateTime QScriptValue::toDateTime () const

Returns the QDateTime representation of this value. If this QScriptValue is not a date, or the value of the date is NaN (Not-a-Number), an invalid QDateTime is returned.

See also isDate().

### qint32 QScriptValue::toInt32 () const

Returns the signed 32-bit integer value of this QScriptValue, using the conversion rules described in ECMA-262 section 9.5, "ToInt32".

See also toNumber() and toUInt32().

### qsreal QScriptValue::toInteger () const

Returns the integer value of this QScriptValue, using the conversion rules described in ECMA-262 section 9.4, "ToInteger".

See also toNumber().

### qsreal QScriptValue::toNumber () const

Returns the number value of this QScriptValue, as defined in ECMA-262 section 9.3, "ToString".

See also isNumber(), toInteger(), toInt32(), toUInt32(), and toUInt16().

### QScriptValue QScriptValue::toObject () const

Returns the object value of this QScriptValue, as defined in ECMA-262 section 9.9, "ToObject".

See also isObject().

### QScriptValue QScriptValue::toPrimitive ( TypeHint hint = NoTypeHint ) const

Returns the primitive value of this QScriptValue, as defined in ECMA-262 section 9.1, "ToPrimitive".

If this QScriptValue is an object, the given hint can be used to indicate the desired primitive type.

### const QMetaObject * QScriptValue::toQMetaObject () const

Returns the QMetaObject value of this QScriptValue.

If this QScriptValue is a QMetaObject, returns the QMetaObject pointer that the QScriptValue represents; otherwise, returns 0.

See also isQMetaObject().

### QObject * QScriptValue::toQObject () const

Returns the QObject value of this QScriptValue.

If this QScriptValue is a QObject, returns the QObject pointer that the QScriptValue represents; otherwise, returns 0.

See also isQObject().

### QRegExp QScriptValue::toRegExp () const

Returns the QRegExp representation of this value. If this QScriptValue is not a regular expression, an empty QRegExp is returned.

See also isRegExp().

### QString QScriptValue::toString () const

Returns the string value of this QScriptValue, as defined in ECMA-262 section 9.8, "ToString".

See also isString().

### quint16 QScriptValue::toUInt16 () const

Returns the unsigned 16-bit integer value of this QScriptValue, using the conversion rules described in ECMA-262 section 9.7, "ToUint16".

See also toNumber().

### quint32 QScriptValue::toUInt32 () const

Returns the unsigned 32-bit integer value of this QScriptValue, using the conversion rules described in ECMA-262 section 9.6, "ToUint32".

See also toNumber() and toInt32().

### QVariant QScriptValue::toVariant () const

Returns the variant value of this QScriptValue.

See also isVariant().

### QScriptValue & QScriptValue::operator= ( const QScriptValue & other )

## Related Non-Members

### T qscriptvalue_cast ( const QScriptValue & value )

Returns the given value converted to the template type `T`.

See also qScriptRegisterMetaType() and QScriptEngine::toScriptValue().