The QJSValue class acts as a container for Qt/JavaScript data types. 更多...
头: | #include <QJSValue> |
qmake: | QT += qml |
Since: | Qt 5.0 |
enum | SpecialValue { UndefinedValue, NullValue } |
QJSValue (SpecialValue value = UndefinedValue) | |
QJSValue (const QJSValue & other ) | |
QJSValue (QJSValue && other ) | |
QJSValue (bool value ) | |
QJSValue (int value ) | |
QJSValue (uint value ) | |
QJSValue (double value ) | |
QJSValue (const QString & value ) | |
QJSValue (const QLatin1String & value ) | |
QJSValue (const char * value ) | |
QJSValue (QV4::ExecutionEngine * e , quint64 val ) | |
~QJSValue () | |
QJSValue | call (const QJSValueList & args = QJSValueList()) |
QJSValue | callAsConstructor (const QJSValueList & args = QJSValueList()) |
QJSValue | callWithInstance (const QJSValue & instance , const QJSValueList & args = QJSValueList()) |
bool | deleteProperty (const QString & name ) |
bool | 等于 (const QJSValue & other ) const |
bool | hasOwnProperty (const QString & name ) const |
bool | hasProperty (const QString & name ) const |
bool | isArray () const |
bool | isBool () const |
bool | isCallable () const |
bool | isDate () const |
bool | isError () 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 | isVariant () const |
QJSValue | property (const QString & name ) const |
QJSValue | property (quint32 arrayIndex ) const |
QJSValue | prototype () const |
void | setProperty (const QString & name , const QJSValue & value ) |
void | setProperty (quint32 arrayIndex , const QJSValue & value ) |
void | setPrototype (const QJSValue & prototype ) |
bool | strictlyEquals (const QJSValue & other ) const |
bool | toBool () const |
QDateTime | toDateTime () const |
qint32 | toInt () const |
double | toNumber () const |
const QMetaObject * | toQMetaObject () const |
QObject * | toQObject () const |
QString | toString () const |
quint32 | toUInt () const |
QVariant | toVariant () const |
QJSValue & | operator= (QJSValue && other ) |
QJSValue & | operator= (const QJSValue & other ) |
The 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 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 (如 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 类。
Object values have an internal
prototype
property, which can be accessed with
prototype
() 和
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 ().
使用 等于 () 或 strictlyEquals () to compare a QJSValue to another.
注意,
QJSValue
其中
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++.
To create an array using QJSValue ,使用 QJSEngine::newArray ():
// Assumes that this class was declared in QML. QJSValue jsArray = engine->newArray(3);
To set individual elements in the array, use the setProperty(quint32 arrayIndex, const QJSValue &value) overload. For example, to fill the array above with integers:
for (int i = 0; i < 3; ++i) { jsArray.setProperty(i, QRandomGenerator::global().generate()); }
To determine the length of the array, access the
"length"
property. To access array elements, use the
property
(quint32 arrayIndex) overload. The following code reads the array we created above back into a list:
QVector<int> integers; const int length = jsArray.property("length").toInt(); for (int i = 0; i < length; ++i) { integers.append(jsArray.property(i).toInt()); }
另请参阅 QJSEngine and QJSValueIterator .
This enum is used to specify a single-valued type.
常量 | 值 | 描述 |
---|---|---|
QJSValue::UndefinedValue
|
1
|
An undefined value. |
QJSValue::NullValue
|
0
|
A null value. |
构造新的 QJSValue with a special value .
构造新的 QJSValue that is a copy of other .
注意,若 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).
Move constructor. Moves from other 到此 QJSValue 对象。
构造新的 QJSValue with a boolean value .
构造新的 QJSValue with a number value .
构造新的 QJSValue with a number value .
构造新的 QJSValue with a number value .
构造新的 QJSValue with a string value .
构造新的 QJSValue with a string value .
构造新的 QJSValue with a string value .
Default constructs an instance of QJSValue.
销毁此 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.
若此 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.
另请参阅 isCallable (), callWithInstance (),和 callAsConstructor ().
创建新的
对象
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.
若此 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.
另请参阅 call () 和 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.
若此 QJSValue is not a function, call () does nothing and returns an undefined QJSValue .
注意,若 instance is not an object, the global object (see QJSEngine::globalObject ()) will be used as the `this' object.
调用
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.
另请参阅 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:
另请参阅 setProperty () 和 hasOwnProperty ().
返回 true,若此 QJSValue 等于 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
字符串
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 或 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).
另请参阅 strictlyEquals ().
Returns true if this object has an own (not prototype-inherited) property of the given name ,否则返回 false。
另请参阅 property () 和 hasProperty ().
Returns true if this object has a property of the given name ,否则返回 false。
另请参阅 property () 和 hasOwnProperty ().
返回 true,若此 QJSValue is an object of the Array class; otherwise returns false.
另请参阅 QJSEngine::newArray ().
返回 true,若此 QJSValue is of the primitive type Boolean; otherwise returns false.
另请参阅 toBool ().
返回 true,若此 QJSValue can be called a function, otherwise returns false.
另请参阅 call ().
返回 true,若此 QJSValue is an object of the Date class; otherwise returns false.
返回 true,若此 QJSValue is an object of the Error class; otherwise returns false.
另请参阅 QJSEngine - Script Exceptions .
返回 true,若此 QJSValue is of the primitive type Null; otherwise returns false.
返回 true,若此 QJSValue is of the primitive type Number; otherwise returns false.
另请参阅 toNumber ().
返回 true,若此 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.
另请参阅 QJSEngine::newObject ().
返回 true,若此 QJSValue 是 QMetaObject ;否则返回 false。
该函数在 Qt 5.8 引入。
另请参阅 toQMetaObject () 和 QJSEngine::newQMetaObject ().
返回 true,若此 QJSValue 是 QObject ;否则返回 false。
Note: This function returns true even if the QObject that this QJSValue wraps has been deleted.
另请参阅 toQObject () 和 QJSEngine::newQObject ().
返回 true,若此 QJSValue is an object of the RegExp class; otherwise returns false.
返回 true,若此 QJSValue is of the primitive type String; otherwise returns false.
另请参阅 toString ().
返回 true,若此 QJSValue is of the primitive type Undefined; otherwise returns false.
返回 true,若此 QJSValue is a variant value; otherwise returns false.
另请参阅 toVariant ().
Returns the value of this QJSValue 's property with the given name . If no such property exists, an undefined QJSValue 被返回。
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
对象)。
To access array elements, use the setProperty(quint32 arrayIndex, const QJSValue &value) overload instead.
另请参阅 setProperty (), hasProperty (),和 QJSValueIterator .
这是重载函数。
Returns the property at the given arrayIndex .
It is possible to access elements in an array in two ways. The first is to use the array index as the property name:
qDebug() << jsValueArray.property(QLatin1String("4")).toString();
The second is to use the overload that takes an index:
qDebug() << jsValueArray.property(4).toString();
Both of these approaches achieve the same result, except that the latter:
若此 QJSValue is not an Array object, this function behaves as if property () was called with the string representation of arrayIndex .
若此
QJSValue
is an object, returns the internal prototype (
__proto__
property) of this object; otherwise returns an undefined
QJSValue
.
另请参阅 setPrototype () 和 isObject ().
Sets the value of this QJSValue 's property with the given name 到给定 value .
若此 QJSValue is not an object, this function does nothing.
若此 QJSValue does not already have a property with name name , a new property is created.
To modify array elements, use the setProperty(quint32 arrayIndex, const QJSValue &value) overload instead.
另请参阅 property () 和 deleteProperty ().
这是重载函数。
Sets the property at the given arrayIndex 到给定 value .
It is possible to modify elements in an array in two ways. The first is to use the array index as the property name:
jsValueArray.setProperty(QLatin1String("4"), value);
The second is to use the overload that takes an index:
jsValueArray.setProperty(4, value);
Both of these approaches achieve the same result, except that the latter:
若此 QJSValue is not an Array object, this function behaves as if setProperty () was called with the string representation of arrayIndex .
另请参阅 property (quint32 arrayIndex) and Working With Arrays .
若此
QJSValue
is an object, sets the internal prototype (
__proto__
property) of this object to be
prototype
; if the
QJSValue
is null, it sets the prototype to null; 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.
另请参阅 prototype () 和 isObject ().
返回 true,若此 QJSValue 等于 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:
类型 | 结果 |
---|---|
Undefined | true |
Null | true |
布尔 | 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 |
字符串 | true if both values are exactly the same sequence of characters, false otherwise |
对象 | true if both values refer to the same object, false otherwise |
另请参阅 等于 ().
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).
另请参阅 isBool ().
返回 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 被返回。
另请参阅 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).
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).
另请参阅 isNumber (), toInt (),和 toUInt ().
* If this QJSValue 是 QMetaObject , returns the QMetaObject pointer * that the QJSValue represents; otherwise, returns 0. * *
该函数在 Qt 5.8 引入。
另请参阅 isQMetaObject ().
若此 QJSValue 是 QObject , returns the QObject pointer that the QJSValue represents; otherwise, returns 0.
若 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 () 返回 true)。
另请参阅 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).
另请参阅 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).
返回 QVariant value of this QJSValue , if it can be converted to a QVariant ;否则返回无效 QVariant . The conversion is performed according to the following table:
Input Type | 结果 |
---|---|
Undefined | An invalid QVariant . |
Null | A QVariant containing a null pointer (QMetaType::Nullptr). |
布尔 | A QVariant containing the value of the boolean. |
Number | A QVariant containing the value of the number. |
字符串 | A QVariant containing the value of the string. |
QVariant 对象 | The result is the QVariant value of the object (no conversion). |
QObject 对象 | 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. |
对象 | The object is converted to a QVariantMap . Each property is converted to a QVariant , recursively; cyclic references are not followed. |
另请参阅 isVariant ().
移动赋值 other 到此 QJSValue 对象。
赋值 other value to this QJSValue .
注意,若 other is an object ( isObject () returns true), only a reference to the underlying object will be assigned; the object itself will not be copied.