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.

Public Types

Public Functions

Related Non-Members


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 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. 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.

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++.

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. 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().

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

See also QScriptEngine and QScriptValueIterator.


Member Type Documentation

enum QScriptValue::PropertyFlag
flags QScriptValue::PropertyFlags

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.
QScriptValue::QObjectMember0x00000020This flag is used to indicate that an existing property is a QObject member (a property or method).
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.
QScriptValue::UserRange0xff000000Flags 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.

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.
QScriptValue::ResolveScope0x02Check the object's own properties first, then search the scope chain.
QScriptValue::ResolveFullResolvePrototype | ResolveScopeCheck 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

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

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


Member Function Documentation

QScriptValue::QScriptValue ()

Constructs an invalid QScriptValue.

QScriptValue::QScriptValue ( const QScriptValue & other )

Constructs a new QScriptValue that is a copy of other.

Note that if other is an object (i.e., isObject() would return true), then only a reference to the underlying object is copied into the new script value (i.e., the object itself is not copied).

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

Constructs a new QScriptValue with the special value and registers it with the script engine.

QScriptValue::QScriptValue ( QScriptEngine * engine, bool value )

Constructs a new QScriptValue with the boolean value and registers it with the script engine.

QScriptValue::QScriptValue ( QScriptEngine * engine, int value )

Constructs a new QScriptValue with the integer value and registers it with the script engine.

QScriptValue::QScriptValue ( QScriptEngine * engine, uint value )

Constructs a new QScriptValue with the unsigned integer value and registers it with the script engine.

QScriptValue::QScriptValue ( QScriptEngine * engine, qsreal value )

Constructs a new QScriptValue with the qsreal value and registers it with the script engine.

QScriptValue::QScriptValue ( QScriptEngine * engine, const QString & value )

Constructs a new QScriptValue with the string value and registers it with the script engine.

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

Constructs a new QScriptValue with the string value and registers it with the script engine.

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.

Calling call() can cause an exception to occur in the script engine; in that case, call() returns the value that was thrown (typically an Error object). You can call QScriptEngine::hasUncaughtException() to determine if an exception occurred.

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.

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

Calling construct() can cause an exception to occur in the script engine; in that case, construct() returns the value that was thrown (typically an Error object). You can call QScriptEngine::hasUncaughtException() to determine if an exception occurred.

See also call() and QScriptEngine::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(), QScriptEngine::newObject(), and QScriptContext::argumentsObject().

QScriptEngine * QScriptValue::engine () const

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

bool QScriptValue::equals ( 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".

Note that if this QScriptValue or the other value are objects, calling this function has side effects on the script engine, since the engine will call the object's valueOf() function (and possibly toString()) in an attempt to convert the object to a primitive value (possibly resulting in an uncaught script exception).

See also strictlyEquals() and lessThan().

bool QScriptValue::instanceOf ( const QScriptValue & other ) const

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

This QScriptValue is considered to be an instance of other if other is a function and the value of the prototype property of other is in the prototype chain of this QScriptValue.

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::nullValue().

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 returns true for such values.

See also toObject() 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.

Note: This function returns true even if the QObject that this QScriptValue wraps has been deleted.

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::undefinedValue().

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".

Note that if this QScriptValue or the other value are objects, calling this function has side effects on the script engine, since the engine will call the object's valueOf() function (and possibly toString()) in an attempt to convert the object to a primitive value (possibly resulting in an uncaught script exception).

See also equals().

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.

If the property is implemented using a getter function (i.e. has the PropertyGetter flag set), calling property() has side-effects on the script engine, since the getter function will be called (possibly resulting in an uncaught script exception). If an exception occurred, property() returns the value that was thrown (typically an Error object).

See also setProperty() and propertyFlags().

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, using the given mode to resolve the property.

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

If this QScriptValue is not an Array object, this function behaves as if property() was called with the string representation of arrayIndex.

QScriptValue::PropertyFlags QScriptValue::propertyFlags ( const QString & name, const ResolveFlags & mode = ResolvePrototype ) const

Returns the flags of the property with the given name, using the given mode to resolve the property.

See also property().

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 = KeepExistingFlags )

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.

If the property is implemented using a setter function (i.e. has the PropertySetter flag set), calling setProperty() has side-effects on the script engine, since the setter function will be called with the given value as argument (possibly resulting in an uncaught script exception).

Note that you cannot specify custom getter or setter functions for built-in properties, such as the length property of Array objects or meta properties of QObject objects.

See also property().

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

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.

If this QScriptValue is not an Array object, this function behaves as if setProperty() was called with the string representation of arrayIndex.

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.

The internal prototype should not be confused with the public property with name "prototype"; the public prototype is usually only set on functions that act as constructors.

See also prototype() and isObject().

bool QScriptValue::strictlyEquals ( 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".

See also equals().

bool QScriptValue::toBoolean () const

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

Note that if this QScriptValue is an object, calling this function has side effects on the script engine, since the engine will call the object's valueOf() function (and possibly toString()) in an attempt to convert the object to a primitive value (possibly resulting in an uncaught script exception).

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".

Note that if this QScriptValue is an object, calling this function has side effects on the script engine, since the engine will call the object's valueOf() function (and possibly toString()) in an attempt to convert the object to a primitive value (possibly resulting in an uncaught script exception).

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".

Note that if this QScriptValue is an object, calling this function has side effects on the script engine, since the engine will call the object's valueOf() function (and possibly toString()) in an attempt to convert the object to a primitive value (possibly resulting in an uncaught script exception).

See also toNumber().

qsreal QScriptValue::toNumber () const

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

Note that if this QScriptValue is an object, calling this function has side effects on the script engine, since the engine will call the object's valueOf() function (and possibly toString()) in an attempt to convert the object to a primitive value (possibly resulting in an uncaught script exception).

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

QScriptValue QScriptValue::toObject () const

Returns the object value of this QScriptValue, if it can be converted to an object; otherwise returns an invalid QScriptValue. The conversion is performed according to the following table:

Input TypeResult
UndefinedAn invalid QScriptValue.
NullAn invalid QScriptValue.
BooleanA new Boolean object whose internal value is set to the value of the boolean.
NumberA new Number object whose internal value is set to the value of the number.
StringA new String object whose internal value is set to the value of the string.
ObjectThe result is the object itself (no conversion).

See also isObject() and QScriptEngine::newObject().

const QMetaObject * QScriptValue::toQMetaObject () const

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

See also isQMetaObject().

QObject * QScriptValue::toQObject () const

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

If the QObject that this QScriptValue wraps has been deleted, this function returns 0 (i.e. it is possible for toQObject() to return 0 even when isQObject() returns true).

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".

Note that if this QScriptValue is an object, calling this function has side effects on the script engine, since the engine will call the object's toString() function (and possibly valueOf()) in an attempt to convert the object to a primitive value (possibly resulting in an uncaught script exception).

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".

Note that if this QScriptValue is an object, calling this function has side effects on the script engine, since the engine will call the object's valueOf() function (and possibly toString()) in an attempt to convert the object to a primitive value (possibly resulting in an uncaught script exception).

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".

Note that if this QScriptValue is an object, calling this function has side effects on the script engine, since the engine will call the object's valueOf() function (and possibly toString()) in an attempt to convert the object to a primitive value (possibly resulting in an uncaught script exception).

See also toNumber() and toInt32().

QVariant QScriptValue::toVariant () const

Returns the QVariant value of this QScriptValue, if it can be converted to a QVariant; otherwise returns an invalid QVariant. The conversion is performed according to the following table:

Input TypeResult
UndefinedAn invalid QVariant.
NullAn invalid QVariant.
BooleanA QVariant containing the value of the boolean.
NumberA QVariant containing the value of the number.
StringA QVariant containing the value of the string.
QVariant ObjectThe result is the QVariant value of the object (no conversion).
QObject ObjectA QVariant containing a pointer to the QObject.
Date ObjectA QVariant containing the date value (toDateTime()).
RegExp ObjectA QVariant containing the regular expression value (toRegExp()).
ObjectIf the value is primitive, then the result is converted to a QVariant according to the above rules; otherwise, an invalid QVariant is returned.

See also isVariant().

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

Assigns the other value to this QScriptValue.

Note that if other is an object (isObject() returns true), only a reference to the underlying object will be assigned; the object itself will not be copied.


Related Non-Members

T qscriptvalue_cast ( const QScriptValue & value )

Returns the given value converted to the template type T.

This function was introduced in Qt 4.3.

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


Copyright © 2008 Trolltech Trademarks
Qt 4.3.5