QJSValue ClassThe QJSValue class acts as a container for Qt/JavaScript data types. More... #include <QJSValue> This class was introduced in Qt 5.0. Public Types
Public Functions
Detailed DescriptionThe QJSValue class acts as a container for Qt/JavaScript data types. QJSValue 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, 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. 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. 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++. See also QJSEngine and QJSValueIterator. Member Type Documentation
|
Constant | Value | Description |
---|---|---|
QJSValue::UndefinedValue | 1 | An undefined value. |
QJSValue::NullValue | 0 | A null value. |
Constructs a new QJSValue with a special value.
Constructs a new QJSValue 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).
Constructs a new QJSValue with a boolean value.
Constructs a new QJSValue with a number value.
Constructs a new QJSValue with a number value.
Constructs a new QJSValue with a number value.
Constructs a new QJSValue with a string value.
Constructs a new QJSValue with a string value.
Constructs a new QJSValue with a string value.
Destroys this QJSValue.
Calls this QJSValue as a function, passing args as arguments to the function, and using the globalObject() as the "this"-object. Returns the value returned from the function.
If this QJSValue is not callable, call() does nothing and returns an undefined QJSValue.
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 isError() on the return value to determine whether an exception occurred.
See also isCallable(), callWithInstance(), and callAsConstructor().
Creates a new Object and calls this QJSValue 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 default constructed object is returned.
If this QJSValue is not a function, callAsConstructor() does nothing and returns an undefined QJSValue.
Calling this function can cause an exception to occur in the script engine; in that case, the value that was thrown (typically an Error object) is returned. You can call isError() on the return value to determine whether an exception occurred.
See also call() and QJSEngine::newObject().
Calls this QJSValue as a function, using instance 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 QJSValue is not a function, call() does nothing and returns an undefined QJSValue.
Note that if instance is not an object, the global object (see QJSEngine::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 isError() on the return value to determine whether an exception occurred.
See also call().
Attempts to delete this object's property of the given name. Returns true if the property was deleted, otherwise returns false.
The behavior of this function is consistent with the JavaScript delete operator. In particular:
See also setProperty() and hasOwnProperty().
Returns true if this QJSValue 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".
This function can return true even if the type of this QJSValue is different from the type of the other value; i.e. the comparison is not strict. For example, comparing the number 9 to the string "9" returns true; comparing an undefined value to a null value returns true; comparing a Number object whose primitive value is 6 to a String object whose primitive value is "6" returns true; and comparing the number 1 to the boolean value true returns true. If you want to perform a comparison without such implicit value conversion, use strictlyEquals().
Note that if this QJSValue 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().
Returns true if this object has an own (not prototype-inherited) property of the given name, otherwise returns false.
See also property() and hasProperty().
Returns true if this object has a property of the given name, otherwise returns false.
See also property() and hasOwnProperty().
Returns true if this QJSValue is an object of the Array class; otherwise returns false.
See also QJSEngine::newArray().
Returns true if this QJSValue is of the primitive type Boolean; otherwise returns false.
See also toBool().
Returns true if this QJSValue can be called a function, otherwise returns false.
See also call().
Returns true if this QJSValue is an object of the Date class; otherwise returns false.
See also QJSEngine::newDate().
Returns true if this QJSValue is an object of the Error class; otherwise returns false.
Returns true if this QJSValue is of the primitive type Null; otherwise returns false.
Returns true if this QJSValue is of the primitive type Number; otherwise returns false.
See also toNumber().
Returns true if this QJSValue 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 QJSEngine::newObject().
Returns true if this QJSValue is a QObject; otherwise returns false.
Note: This function returns true even if the QObject that this QJSValue wraps has been deleted.
See also toQObject() and QJSEngine::newQObject().
Returns true if this QJSValue is an object of the RegExp class; otherwise returns false.
Returns true if this QJSValue is of the primitive type String; otherwise returns false.
See also toString().
Returns true if this QJSValue is of the primitive type Undefined; otherwise returns false.
Returns true if this QJSValue is a variant value; otherwise returns false.
See also toVariant().
Returns the value of this QJSValue's property with the given name. If no such property exists, an undefined QJSValue 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(), hasProperty(), and QJSValueIterator.
This is an overloaded function.
Returns the property at the given arrayIndex.
This function is provided for convenience and performance when working with array objects.
If this QJSValue is not an Array object, this function behaves as if property() was called with the string representation of arrayIndex.
If this QJSValue is an object, returns the internal prototype (__proto__ property) of this object; otherwise returns an undefined QJSValue.
See also setPrototype() and isObject().
Sets the value of this QJSValue's property with the given name to the given value.
If this QJSValue is not an object, this function does nothing.
If this QJSValue does not already have a property with name name, a new property is created.
See also property() and deleteProperty().
This is an overloaded function.
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 QJSValue is not an Array object, this function behaves as if setProperty() was called with the string representation of arrayIndex.
If this QJSValue 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().
Returns true if this QJSValue 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".
If the type of this QJSValue is different from the type of the other value, this function returns false. If the types are equal, the result depends on the type, as shown in the following table:
Type | Result |
---|---|
Undefined | true |
Null | true |
Boolean | true if both values are true, false otherwise |
Number | false if either value is NaN (Not-a-Number); true if values are equal, false otherwise |
String | true if both values are exactly the same sequence of characters, false otherwise |
Object | true if both values refer to the same object, false otherwise |
See also equals().
Returns the boolean value of this QJSValue, using the conversion rules described in ECMA-262 section 9.2, "ToBoolean".
Note that if this QJSValue 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 isBool().
Returns a QDateTime representation of this value, in local time. If this QJSValue is not a date, or the value of the date is NaN (Not-a-Number), an invalid QDateTime is returned.
See also isDate().
Returns the signed 32-bit integer value of this QJSValue, using the conversion rules described in ECMA-262 section 9.5, "ToInt32".
Note that if this QJSValue 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 toUInt().
Returns the number value of this QJSValue, as defined in ECMA-262 section 9.3, "ToNumber".
Note that if this QJSValue 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(), toInt(), and toUInt().
If this QJSValue is a QObject, returns the QObject pointer that the QJSValue represents; otherwise, returns 0.
If the QObject that this QJSValue 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().
Returns the string value of this QJSValue, as defined in ECMA-262 section 9.8, "ToString".
Note that if this QJSValue 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().
Returns the unsigned 32-bit integer value of this QJSValue, using the conversion rules described in ECMA-262 section 9.6, "ToUint32".
Note that if this QJSValue 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 toInt().
Returns the QVariant value of this QJSValue, if it can be converted to a QVariant; otherwise returns an invalid QVariant. The conversion is performed according to the following table:
Input Type | Result |
---|---|
Undefined | An invalid QVariant. |
Null | A QVariant containing a null pointer (QMetaType::VoidStar). |
Boolean | A QVariant containing the value of the boolean. |
Number | A QVariant containing the value of the number. |
String | A QVariant containing the value of the string. |
QVariant Object | The result is the QVariant value of the object (no conversion). |
QObject Object | A QVariant containing a pointer to the QObject. |
Date Object | A QVariant containing the date value (toDateTime()). |
RegExp Object | A QVariant containing the regular expression value. |
Array Object | The array is converted to a QVariantList. Each element is converted to a QVariant, recursively; cyclic references are not followed. |
Object | The object is converted to a QVariantMap. Each property is converted to a QVariant, recursively; cyclic references are not followed. |
See also isVariant().
Assigns the other value to this QJSValue.
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.