def
call
([args=QJSValueList()])
def
callAsConstructor
([args=QJSValueList()])
def
callWithInstance
(instance[, args=QJSValueList()])
def
deleteProperty
(name)
def
engine
()
def
等于
(other)
def
errorType
()
def
hasOwnProperty
(name)
def
hasProperty
(name)
def
isArray
()
def
isBool
()
def
isCallable
()
def
isDate
()
def
isError
()
def
isNull
()
def
isNumber
()
def
isObject
()
def
isQMetaObject
()
def
isQObject
()
def
isRegExp
()
def
isString
()
def
isUndefined
()
def
isVariant
()
def
特性
(arrayIndex)
def
特性
(name)
def
prototype
()
def
setProperty
(arrayIndex, value)
def
setProperty
(name, value)
def
setPrototype
(prototype)
def
strictlyEquals
(other)
def
toBool
()
def
toDateTime
()
def
toInt
()
def
toNumber
()
def
toQMetaObject
()
def
toQObject
()
def
toString
()
def
toUInt
()
def
toVariant
()
QJSValuesupports 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 asQVariantandQObject.For the object-based types (including Date and RegExp), use the newT() functions in
QJSEngine(e.g.newObject()) to create aQJSValueof the desired type. For the primitive types, use one of theQJSValueconstructor overloads. For other types, e.g. registered gadget types such asQPoint, you can usetoScriptValue.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 aQJSValueto another type. You can also use the generic function.Object values have zero or more properties which are themselves QJSValues. Use
setProperty()to set a property of an object, and callproperty()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
prototypeproperty, which can be accessed withprototype()andsetPrototype().Function objects (objects for which
isCallable()) returns true) can be invoked by callingcall(). Constructor functions can be used to construct new objects by callingcallAsConstructor().使用
equals()orstrictlyEquals()to compare aQJSValueto another.注意:
QJSValue其中isObject()is true only carries a reference to an actual object; copying theQJSValuewill 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 afor-instatement in script code, orQJSValueIteratorin C++.
To create an array using
QJSValue,使用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 theproperty(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()); }
QJSValue
(
[
value=UndefinedValue
]
)
¶
QJSValue(value)
QJSValue(other)
QJSValue(value)
QJSValue(str)
QJSValue(value)
QJSValue(value)
QJSValue(value)
- param str
str
- param other
- param value
SpecialValue
构造新
QJSValue
with a special
value
.
构造新
QJSValue
with a boolean
value
.
构造新
QJSValue
with a string
value
.
构造新
QJSValue
with a number
value
.
构造新
QJSValue
with a number
value
.
构造新
QJSValue
with a number
value
.
PySide2.QtQml.QJSValue.
SpecialValue
¶
This enum is used to specify a single-valued type.
|
常量 |
描述 |
|---|---|
|
QJSValue.UndefinedValue |
An undefined value. |
|
QJSValue.NullValue |
A null value. |
PySide2.QtQml.QJSValue.
ErrorType
¶
Use this enum for JavaScript language-specific types of Error objects.
They may be useful when emulating language features in C++ requires the use of specialized exception types. In addition, they may help to more clearly communicate certain typical conditions, instead of throwing a generic JavaScript exception. For example, code that deals with networking and resource locators may find it useful to propagate errors related to malformed locators using the type.
|
常量 |
描述 |
|---|---|
|
QJSValue.GenericError |
A generic Error object, but not of a specific sub-type. |
|
QJSValue.RangeError |
A value did not match the expected set or range. |
|
QJSValue.ReferenceError |
A non-existing variable referenced. |
|
QJSValue.SyntaxError |
An invalid token or sequence of tokens was encountered that does not conform with the syntax of the language. |
|
QJSValue.TypeError |
An operand or argument is incompatible with the type expected. |
|
QJSValue.URIError |
A URI handling function was used incorrectly or the URI provided is malformed. |
New in version 5.12.
PySide2.QtQml.QJSValue.
call
(
[
args=QJSValueList()
]
)
¶
args –
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, does nothing and returns an undefined
QJSValue
.
Calling can cause an exception to occur in the script engine; in that case, returns the value that was thrown (typically an
Error
object). You can call
isError()
on the return value to determine whether an exception occurred.
PySide2.QtQml.QJSValue.
callAsConstructor
(
[
args=QJSValueList()
]
)
¶
args –
创建新
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.
若此
QJSValue
is not a function, 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.
另请参阅
PySide2.QtQml.QJSValue.
callWithInstance
(
instance
[
,
args=QJSValueList()
]
)
¶
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
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.
另请参阅
PySide2.QtQml.QJSValue.
deleteProperty
(
name
)
¶
name – unicode
bool
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:
Non-configurable properties cannot be deleted.
This function will return true even if this object doesn’t have a property of the given
name
(i.e., non-existent properties are “trivially deletable”).
If this object doesn’t have an own property of the given
name
, but an object in the
prototype()
chain does, the prototype object’s property is not deleted, and this function returns true.
PySide2.QtQml.QJSValue.
engine
(
)
¶
注意
此函数被弃用。
返回
QJSEngine
that created this
QJSValue
, or 0 if this
QJSValue
is invalid or the value is not associated with a particular engine.
PySide2.QtQml.QJSValue.
等于
(
other
)
¶
other
–
QJSValue
bool
返回 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).
另请参阅
PySide2.QtQml.QJSValue.
errorType
(
)
¶
Returns the error type this
QJSValue
represents if it is an Error object. Otherwise, returns
NoError."
另请参阅
isError()
QJSEngine
-
Script
异常
PySide2.QtQml.QJSValue.
hasOwnProperty
(
name
)
¶
name – unicode
bool
Returns true if this object has an own (not prototype-inherited) property of the given
name
,否则返回 false。
另请参阅
PySide2.QtQml.QJSValue.
hasProperty
(
name
)
¶
name – unicode
bool
Returns true if this object has a property of the given
name
,否则返回 false。
PySide2.QtQml.QJSValue.
isArray
(
)
¶
bool
返回 true,若此
QJSValue
is an object of the Array class; otherwise returns false.
另请参阅
PySide2.QtQml.QJSValue.
isBool
(
)
¶
bool
返回 true,若此
QJSValue
is of the primitive type Boolean; otherwise returns false.
另请参阅
PySide2.QtQml.QJSValue.
isCallable
(
)
¶
bool
返回 true,若此
QJSValue
can be called a function, otherwise returns false.
另请参阅
PySide2.QtQml.QJSValue.
isDate
(
)
¶
bool
返回 true,若此
QJSValue
is an object of the Date class; otherwise returns false.
PySide2.QtQml.QJSValue.
isError
(
)
¶
bool
返回 true,若此
QJSValue
is an object of the Error class; otherwise returns false.
另请参阅
errorType()
QJSEngine
-
Script
异常
PySide2.QtQml.QJSValue.
isNull
(
)
¶
bool
返回 true,若此
QJSValue
is of the primitive type Null; otherwise returns false.
PySide2.QtQml.QJSValue.
isNumber
(
)
¶
bool
返回 true,若此
QJSValue
is of the primitive type Number; otherwise returns false.
另请参阅
PySide2.QtQml.QJSValue.
isObject
(
)
¶
bool
返回 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.
另请参阅
PySide2.QtQml.QJSValue.
isQMetaObject
(
)
¶
bool
返回 true,若此
QJSValue
是
QMetaObject
;否则返回 false。
PySide2.QtQml.QJSValue.
isQObject
(
)
¶
bool
返回 true,若此
QJSValue
是
QObject
;否则返回 false。
Note: This function returns true even if the
QObject
that this
QJSValue
wraps has been deleted.
另请参阅
PySide2.QtQml.QJSValue.
isRegExp
(
)
¶
bool
返回 true,若此
QJSValue
is an object of the RegExp class; otherwise returns false.
PySide2.QtQml.QJSValue.
isString
(
)
¶
bool
返回 true,若此
QJSValue
is of the primitive type String; otherwise returns false.
另请参阅
PySide2.QtQml.QJSValue.
isUndefined
(
)
¶
bool
返回 true,若此
QJSValue
is of the primitive type Undefined; otherwise returns false.
PySide2.QtQml.QJSValue.
isVariant
(
)
¶
bool
返回 true,若此
QJSValue
is a variant value; otherwise returns false.
另请参阅
PySide2.QtQml.QJSValue.
特性
(
arrayIndex
)
¶
arrayIndex
–
quint32
这是重载函数。
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:
Is easier to use (can use an integer directly)
Is faster (no conversion to integer)
若此
QJSValue
is not an Array object, this function behaves as if
property()
was called with the string representation of
arrayIndex
.
PySide2.QtQml.QJSValue.
prototype
(
)
¶
若此
QJSValue
is an object, returns the internal prototype (
__proto__
property) of this object; otherwise returns an undefined
QJSValue
.
另请参阅
PySide2.QtQml.QJSValue.
setProperty
(
arrayIndex
,
value
)
¶
arrayIndex
–
quint32
value
–
QJSValue
PySide2.QtQml.QJSValue.
setPrototype
(
prototype
)
¶
prototype
–
QJSValue
若此
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.
另请参阅
PySide2.QtQml.QJSValue.
strictlyEquals
(
other
)
¶
other
–
QJSValue
bool
返回 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:
|
Type |
结果 |
|
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 |
|
Object |
true if both values refer to the same object, false otherwise |
另请参阅
PySide2.QtQml.QJSValue.
toBool
(
)
¶
bool
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).
另请参阅
PySide2.QtQml.QJSValue.
toDateTime
(
)
¶
QDateTime
返回
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
被返回。
另请参阅
PySide2.QtQml.QJSValue.
toInt
(
)
¶
qint32
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).
另请参阅
PySide2.QtQml.QJSValue.
toNumber
(
)
¶
double
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).
另请参阅
PySide2.QtQml.QJSValue.
toQMetaObject
(
)
¶
QMetaObject
* If this
QJSValue
是
QMetaObject
, returns the
QMetaObject
pointer * that the
QJSValue
represents; otherwise, returns
None
. * *
另请参阅
PySide2.QtQml.QJSValue.
toQObject
(
)
¶
QObject
若此
QJSValue
是
QObject
, returns the
QObject
pointer that the
QJSValue
represents; otherwise, returns
None
.
若
QObject
that this
QJSValue
wraps has been deleted, this function returns
None
(i.e. it is possible for to return
None
even when
isQObject()
returns true).
另请参阅
PySide2.QtQml.QJSValue.
toString
(
)
¶
unicode
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 function (and possibly valueOf()) in an attempt to convert the object to a primitive value (possibly resulting in an uncaught script exception).
另请参阅
PySide2.QtQml.QJSValue.
toUInt
(
)
¶
quint32
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).
另请参阅
PySide2.QtQml.QJSValue.
toVariant
(
)
¶
object
返回
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 |
结果 |
|
Undefined |
An invalid
|
|
Null |
A
|
|
布尔 |
A
|
|
Number |
A
|
|
字符串 |
A
|
|
|
The result is the
|
|
|
A
|
|
Date Object |
A
|
|
RegExp Object |
A
|
|
Array Object |
The array is converted to a
|
|
Object |
The object is converted to a
|
另请参阅