Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QJSValue Class Reference

The QJSValue class acts as a container for Qt/JavaScript data types. More...

#include <qjsvalue.h>

+ Collaboration diagram for QJSValue:

Public Types

enum  SpecialValue { NullValue , UndefinedValue }
 This enum is used to specify a single-valued type. More...
 
enum  ErrorType {
  NoError , GenericError , EvalError , RangeError ,
  ReferenceError , SyntaxError , TypeError , URIError
}
 
enum  ObjectConversionBehavior { ConvertJSObjects , RetainJSObjects }
 This enum is used to specify how JavaScript objects and symbols without an equivalent native Qt type should be treated when converting to QVariant. More...
 

Public Member Functions

 QJSValue (SpecialValue value=UndefinedValue)
 Constructs a new QJSValue with a special value.
 
 ~QJSValue ()
 Destroys this QJSValue.
 
 QJSValue (const QJSValue &other)
 Constructs a new QJSValue that is a copy of other.
 
 QJSValue (QJSValue &&other)
 Move constructor.
 
QJSValueoperator= (QJSValue &&other)
 Move-assigns other to this QJSValue object.
 
 QJSValue (bool value)
 Constructs a new QJSValue with a boolean value.
 
 QJSValue (int value)
 Constructs a new QJSValue with a number value.
 
 QJSValue (uint value)
 Constructs a new QJSValue with a number value.
 
 QJSValue (double value)
 Constructs a new QJSValue with a number value.
 
 QJSValue (const QString &value)
 Constructs a new QJSValue with a string value.
 
 QJSValue (const QLatin1String &value)
 Constructs a new QJSValue with a string value.
 
QT_ASCII_CAST_WARN QJSValue (const char *str)
 Constructs a new QJSValue with a string value.
 
QJSValueoperator= (const QJSValue &other)
 Assigns the other value to this QJSValue.
 
 QJSValue (QJSPrimitiveValue &&value)
 
 QJSValue (QJSManagedValue &&value)
 
bool isBool () const
 Returns true if this QJSValue is of the primitive type Boolean; otherwise returns false.
 
bool isNumber () const
 Returns true if this QJSValue is of the primitive type Number; otherwise returns false.
 
bool isNull () const
 Returns true if this QJSValue is of the primitive type Null; otherwise returns false.
 
bool isString () const
 Returns true if this QJSValue is of the primitive type String; otherwise returns false.
 
bool isUndefined () const
 Returns true if this QJSValue is of the primitive type Undefined or if the managed value has been cleared (by deleting the engine).
 
bool isQObject () const
 Returns true if this QJSValue is a QObject; otherwise returns false.
 
bool isQMetaObject () const
 
bool isObject () const
 Returns true if this QJSValue is of the Object type; otherwise returns false.
 
bool isDate () const
 Returns true if this QJSValue is an object of the Date class; otherwise returns false.
 
bool isRegExp () const
 Returns true if this QJSValue is an object of the RegExp class; otherwise returns false.
 
bool isArray () const
 Returns true if this QJSValue is an object of the Array class; otherwise returns false.
 
bool isError () const
 Returns true if this QJSValue is an object of the Error class; otherwise returns false.
 
bool isUrl () const
 Returns true if this QJSValue is an object of the URL JavaScript class; otherwise returns false.
 
QString toString () const
 Returns the string value of this QJSValue, as defined in \l{ECMA-262} section 9.8, "ToString".
 
double toNumber () const
 Returns the number value of this QJSValue, as defined in \l{ECMA-262} section 9.3, "ToNumber".
 
qint32 toInt () const
 Returns the signed 32-bit integer value of this QJSValue, using the conversion rules described in \l{ECMA-262} section 9.5, "ToInt32".
 
quint32 toUInt () const
 Returns the unsigned 32-bit integer value of this QJSValue, using the conversion rules described in \l{ECMA-262} section 9.6, "ToUint32".
 
bool toBool () const
 Returns the boolean value of this QJSValue, using the conversion rules described in \l{ECMA-262} section 9.2, "ToBoolean".
 
QVariant toVariant () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns toVariant(ConvertJSObjects).
 
QVariant toVariant (ObjectConversionBehavior behavior) const
 Returns the QVariant value of this QJSValue, if it can be converted to a QVariant; otherwise returns an invalid QVariant.
 
QJSPrimitiveValue toPrimitive () const
 Converts the value to a QJSPrimitiveValue.
 
QObjecttoQObject () const
 If this QJSValue is a QObject, returns the QObject pointer that the QJSValue represents; otherwise, returns \nullptr.
 
const QMetaObjecttoQMetaObject () const
 
QDateTime toDateTime () const
 Returns a QDateTime representation of this value, in local time.
 
bool equals (const QJSValue &other) const
 Returns true if this QJSValue is equal to other, otherwise returns false.
 
bool strictlyEquals (const QJSValue &other) const
 Returns true if this QJSValue is equal to other using strict comparison (no conversion), otherwise returns false.
 
QJSValue prototype () const
 If this QJSValue is an object, returns the internal prototype ({proto} property) of this object; otherwise returns an undefined QJSValue.
 
void setPrototype (const QJSValue &prototype)
 If this 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.
 
QJSValue property (const QString &name) const
 Returns the value of this QJSValue's property with the given name.
 
void setProperty (const QString &name, const QJSValue &value)
 Sets the value of this QJSValue's property with the given name to the given value.
 
bool hasProperty (const QString &name) const
 Returns true if this object has a property of the given name, otherwise returns false.
 
bool hasOwnProperty (const QString &name) const
 Returns true if this object has an own (not prototype-inherited) property of the given name, otherwise returns false.
 
QJSValue property (quint32 arrayIndex) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns the property at the given arrayIndex.
 
void setProperty (quint32 arrayIndex, const QJSValue &value)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Sets the property at the given arrayIndex to the given value.
 
bool deleteProperty (const QString &name)
 Attempts to delete this object's property of the given name.
 
bool isCallable () const
 Returns true if this QJSValue is a function, otherwise returns false.
 
QJSValue call (const QJSValueList &args=QJSValueList()) const
 Calls this QJSValue as a function, passing args as arguments to the function, and using the globalObject() as the "this"-object.
 
QJSValue callWithInstance (const QJSValue &instance, const QJSValueList &args=QJSValueList()) const
 Calls this QJSValue as a function, using instance as the ‘this’ object in the function call, and passing args as arguments to the function.
 
QJSValue callAsConstructor (const QJSValueList &args=QJSValueList()) const
 Creates a new {Object} and calls this QJSValue as a constructor, using the created object as the ‘this’ object and passing args as arguments.
 
ErrorType errorType () const
 

Friends

class QJSValuePrivate
 

Related Symbols

(Note that these are not member symbols.)

 QJSValueList
 This is a typedef for a QList<QJSValue>.
 

Detailed Description

The QJSValue class acts as a container for Qt/JavaScript data types.

Since
5.0

\inmodule QtQml

QJSValue supports the types defined in the \l{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 (e.g. 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.

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, QJSValueIterator

Definition at line 30 of file qjsvalue.h.

Member Enumeration Documentation

◆ ErrorType

Since
5.12

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

\omitvalue NoError \value GenericError A generic Error object, but not of a specific sub-type. \omitvalue EvalError \value RangeError A value did not match the expected set or range. \value ReferenceError A non-existing variable referenced. \value SyntaxError An invalid token or sequence of tokens was encountered that does not conform with the syntax of the language. \value TypeError An operand or argument is incompatible with the type expected. \value URIError A URI handling function was used incorrectly or the URI provided is malformed.

Enumerator
NoError 
GenericError 
EvalError 
RangeError 
ReferenceError 
SyntaxError 
TypeError 
URIError 

Definition at line 38 of file qjsvalue.h.

◆ ObjectConversionBehavior

This enum is used to specify how JavaScript objects and symbols without an equivalent native Qt type should be treated when converting to QVariant.

\value ConvertJSObjects A best-effort, possibly lossy, conversion is attempted. Symbols are converted to QString.

\value RetainJSObjects The value is retained as QJSValue wrapped in QVariant.

Enumerator
ConvertJSObjects 
RetainJSObjects 

Definition at line 49 of file qjsvalue.h.

◆ SpecialValue

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

\value UndefinedValue An undefined value.

\value NullValue A null value.

Enumerator
NullValue 
UndefinedValue 

Definition at line 33 of file qjsvalue.h.

Constructor & Destructor Documentation

◆ QJSValue() [1/12]

QJSValue::QJSValue ( SpecialValue value = UndefinedValue)

Constructs a new QJSValue with a special value.

Definition at line 219 of file qjsvalue.cpp.

Referenced by call(), callAsConstructor(), callWithInstance(), property(), property(), and prototype().

+ Here is the caller graph for this function:

◆ ~QJSValue()

QJSValue::~QJSValue ( )

Destroys this QJSValue.

Definition at line 282 of file qjsvalue.cpp.

References QJSValuePrivate::free().

+ Here is the call graph for this function:

◆ QJSValue() [2/12]

QJSValue::QJSValue ( const QJSValue & other)

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

Definition at line 247 of file qjsvalue.cpp.

References QJSValuePrivate::BoolValue, QJSValuePrivate::DoublePtr, QJSValuePrivate::doublePtr(), QJSValuePrivate::encode(), QJSValuePrivate::IntValue, QJSValuePrivate::Null, QJSValuePrivate::QStringPtr, QJSValuePrivate::qStringPtr(), QJSValuePrivate::QV4ValuePtr, QJSValuePrivate::qv4ValuePtr(), QJSValuePrivate::tag(), and QJSValuePrivate::Undefined.

+ Here is the call graph for this function:

◆ QJSValue() [3/12]

QJSValue::QJSValue ( QJSValue && other)
inline

Move constructor.

Moves from other into this QJSValue object.

Definition at line 59 of file qjsvalue.h.

References other().

+ Here is the call graph for this function:

◆ QJSValue() [4/12]

QJSValue::QJSValue ( bool value)

Constructs a new QJSValue with a boolean value.

Definition at line 184 of file qjsvalue.cpp.

◆ QJSValue() [5/12]

QJSValue::QJSValue ( int value)

Constructs a new QJSValue with a number value.

Definition at line 191 of file qjsvalue.cpp.

◆ QJSValue() [6/12]

QJSValue::QJSValue ( uint value)

Constructs a new QJSValue with a number value.

Definition at line 198 of file qjsvalue.cpp.

◆ QJSValue() [7/12]

QJSValue::QJSValue ( double value)

Constructs a new QJSValue with a number value.

Definition at line 205 of file qjsvalue.cpp.

◆ QJSValue() [8/12]

QJSValue::QJSValue ( const QString & value)

Constructs a new QJSValue with a string value.

Definition at line 212 of file qjsvalue.cpp.

◆ QJSValue() [9/12]

QJSValue::QJSValue ( const QLatin1String & value)

Constructs a new QJSValue with a string value.

Definition at line 227 of file qjsvalue.cpp.

◆ QJSValue() [10/12]

QJSValue::QJSValue ( const char * str)

Constructs a new QJSValue with a string value.

Definition at line 235 of file qjsvalue.cpp.

◆ QJSValue() [11/12]

QJSValue::QJSValue ( QJSPrimitiveValue && value)
explicit

Definition at line 917 of file qjsvalue.cpp.

References QJSPrimitiveValue::Boolean, QJSPrimitiveValue::Double, QJSValuePrivate::encode(), QJSValuePrivate::encodeNull(), QJSValuePrivate::encodeUndefined(), QJSPrimitiveValue::Integer, QJSPrimitiveValue::Null, QJSPrimitiveValue::String, and QJSPrimitiveValue::Undefined.

+ Here is the call graph for this function:

◆ QJSValue() [12/12]

QJSValue::QJSValue ( QJSManagedValue && value)
explicit

Definition at line 943 of file qjsvalue.cpp.

References QJSValuePrivate::adoptPersistentValue(), QJSValuePrivate::encode(), QV4::PersistentValueStorage::free(), and QV4::Encode::undefined().

+ Here is the call graph for this function:

Member Function Documentation

◆ call()

QJSValue QJSValue::call ( const QJSValueList & args = QJSValueList()) const

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(), callAsConstructor()

Definition at line 705 of file qjsvalue.cpp.

References QJSValue(), args, QList< T >::at(), QJSValuePrivate::checkEngine(), QJSValuePrivate::convertToReturnedValue(), engine, QJSValuePrivate::engine(), QJSValuePrivate::fromReturnedValue(), QJSEngine::globalObject(), i, QJSEngine::isInterrupted(), QJSEngine::newErrorObject(), Q_ASSERT, QStringLiteral, qWarning, and QList< T >::size().

Referenced by QQuickSpinBoxPrivate::evaluateTextFromValue(), QQuickSpinBoxPrivate::evaluateValueFromText(), QQuickStackView::find(), QQuickTableViewPrivate::getColumnWidth(), and QQuickTableViewPrivate::getRowHeight().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ callAsConstructor()

QJSValue QJSValue::callAsConstructor ( const QJSValueList & args = QJSValueList()) const

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(), QJSEngine::newObject()

Definition at line 806 of file qjsvalue.cpp.

References QJSValue(), args, QList< T >::at(), QJSValuePrivate::checkEngine(), QJSValuePrivate::convertToReturnedValue(), engine, QJSValuePrivate::engine(), QJSValuePrivate::fromReturnedValue(), i, QJSEngine::isInterrupted(), QJSEngine::newErrorObject(), Q_ASSERT, QStringLiteral, qWarning, and QList< T >::size().

+ Here is the call graph for this function:

◆ callWithInstance()

QJSValue QJSValue::callWithInstance ( const QJSValue & instance,
const QJSValueList & args = QJSValueList() ) const

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 \l{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()

Definition at line 754 of file qjsvalue.cpp.

References QJSValue(), args, QList< T >::at(), QJSValuePrivate::checkEngine(), QJSValuePrivate::convertToReturnedValue(), engine, QJSValuePrivate::engine(), QJSValuePrivate::fromReturnedValue(), i, QJSEngine::isInterrupted(), QJSEngine::newErrorObject(), Q_ASSERT, QStringLiteral, qWarning, and QList< T >::size().

+ Here is the call graph for this function:

◆ deleteProperty()

bool QJSValue::deleteProperty ( const QString & name)

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:

\list

  • 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. \endlist
See also
setProperty(), hasOwnProperty()

Definition at line 1249 of file qjsvalue.cpp.

References QJSValuePrivate::asReturnedValue(), engine, QJSValuePrivate::engine(), and o.

+ Here is the call graph for this function:

◆ equals()

bool QJSValue::equals ( const QJSValue & other) const

Returns true if this QJSValue is equal to other, otherwise returns false.

The comparison follows the behavior described in \l{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()

Definition at line 998 of file qjsvalue.cpp.

References QJSValuePrivate::asQString(), QJSValuePrivate::asReturnedValue(), QV4::Runtime::CompareEqual::call(), js_equal(), and other().

+ Here is the call graph for this function:

◆ errorType()

QJSValue::ErrorType QJSValue::errorType ( ) const
Since
5.12 Returns the error type this QJSValue represents if it is an Error object. Otherwise, returns NoError."
See also
isError(), {QJSEngine::Script Exceptions}{QJSEngine - Script Exceptions}

Definition at line 395 of file qjsvalue.cpp.

References error, EvalError, GenericError, NoError, RangeError, ReferenceError, SyntaxError, TypeError, and URIError.

◆ hasOwnProperty()

bool QJSValue::hasOwnProperty ( const QString & name) const

Returns true if this object has an own (not prototype-inherited) property of the given name, otherwise returns false.

See also
property(), hasProperty()

Definition at line 1291 of file qjsvalue.cpp.

References QJSValuePrivate::asReturnedValue(), QV4::Attr_Invalid, engine, QJSValuePrivate::engine(), and o.

+ Here is the call graph for this function:

◆ hasProperty()

bool QJSValue::hasProperty ( const QString & name) const

Returns true if this object has a property of the given name, otherwise returns false.

See also
property(), hasOwnProperty()

Definition at line 1270 of file qjsvalue.cpp.

References QJSValuePrivate::asReturnedValue(), engine, QJSValuePrivate::engine(), and o.

+ Here is the call graph for this function:

◆ isArray()

bool QJSValue::isArray ( ) const

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

See also
QJSEngine::newArray()

Definition at line 425 of file qjsvalue.cpp.

◆ isBool()

bool QJSValue::isBool ( ) const

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

See also
toBool()

Definition at line 293 of file qjsvalue.cpp.

References QJSValuePrivate::BoolValue, and QJSValuePrivate::tag().

+ Here is the call graph for this function:

◆ isCallable()

bool QJSValue::isCallable ( ) const

Returns true if this QJSValue is a function, otherwise returns false.

See also
call()

Definition at line 450 of file qjsvalue.cpp.

Referenced by QQuickSpinBoxPrivate::evaluateTextFromValue(), QQuickSpinBoxPrivate::evaluateValueFromText(), QQuickTableViewPrivate::getColumnWidth(), ListLayout::getRoleOrCreate(), QQuickTableViewPrivate::getRowHeight(), QQuickSpinBox::setTextFromValue(), and QQuickSpinBox::setValueFromText().

+ Here is the caller graph for this function:

◆ isDate()

bool QJSValue::isDate ( ) const

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

Definition at line 1369 of file qjsvalue.cpp.

◆ isError()

bool QJSValue::isError ( ) const

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

See also
errorType(), {QJSEngine::Script Exceptions}{QJSEngine - Script Exceptions}

Definition at line 371 of file qjsvalue.cpp.

◆ isNull()

bool QJSValue::isNull ( ) const

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

Definition at line 321 of file qjsvalue.cpp.

References QJSValuePrivate::Null, and QJSValuePrivate::tag().

Referenced by QQuickRectangle::setGradient().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isNumber()

bool QJSValue::isNumber ( ) const

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

See also
toNumber()

Definition at line 304 of file qjsvalue.cpp.

References QJSValuePrivate::DoublePtr, QJSValuePrivate::IntValue, and QJSValuePrivate::tag().

Referenced by QQuickRectangle::setGradient().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isObject()

bool QJSValue::isObject ( ) const

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

Definition at line 439 of file qjsvalue.cpp.

◆ isQMetaObject()

bool QJSValue::isQMetaObject ( ) const
Since
5.8

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

See also
toQMetaObject(), QJSEngine::newQMetaObject()

Definition at line 1405 of file qjsvalue.cpp.

◆ isQObject()

bool QJSValue::isQObject ( ) const

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(), QJSEngine::newQObject()

Definition at line 1392 of file qjsvalue.cpp.

Referenced by QQuickRectangle::setGradient().

+ Here is the caller graph for this function:

◆ isRegExp()

bool QJSValue::isRegExp ( ) const

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

Definition at line 1378 of file qjsvalue.cpp.

◆ isString()

bool QJSValue::isString ( ) const

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

See also
toString()

Definition at line 332 of file qjsvalue.cpp.

References QJSValuePrivate::QStringPtr, QJSValuePrivate::QV4ValuePtr, QJSValuePrivate::qv4ValuePtr(), and QJSValuePrivate::tag().

Referenced by QQuickRectangle::setGradient().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isUndefined()

bool QJSValue::isUndefined ( ) const

Returns true if this QJSValue is of the primitive type Undefined or if the managed value has been cleared (by deleting the engine).

Otherwise returns false.

Definition at line 351 of file qjsvalue.cpp.

References QJSValuePrivate::QV4ValuePtr, QJSValuePrivate::qv4ValuePtr(), QJSValuePrivate::tag(), and QJSValuePrivate::Undefined.

Referenced by QQuickTableViewPrivate::getColumnWidth(), QQuickTableViewPrivate::getRowHeight(), QQmlPrivate::isUndefined(), and QQuickRectangle::setGradient().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isUrl()

bool QJSValue::isUrl ( ) const

Returns true if this QJSValue is an object of the URL JavaScript class; otherwise returns false.

Note
For a QJSValue that contains a QUrl, this function returns false. However, {toVariant().value<QUrl>()} works in both cases.

Definition at line 383 of file qjsvalue.cpp.

◆ operator=() [1/2]

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

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.

Definition at line 901 of file qjsvalue.cpp.

References QJSValuePrivate::asQString(), QJSValuePrivate::asReturnedValue(), QJSValuePrivate::free(), other(), QJSValuePrivate::setString(), and QJSValuePrivate::setValue().

+ Here is the call graph for this function:

◆ operator=() [2/2]

QJSValue & QJSValue::operator= ( QJSValue && other)
inline

Move-assigns other to this QJSValue object.

Definition at line 60 of file qjsvalue.h.

References d, and other().

+ Here is the call graph for this function:

◆ property() [1/2]

QJSValue QJSValue::property ( const QString & name) const

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

To access array elements, use the \l {QJSValue::}{setProperty(quint32 arrayIndex, const QJSValue &value)} overload instead.

See also
setProperty(), hasProperty(), QJSValueIterator

Definition at line 1072 of file qjsvalue.cpp.

References QJSValue(), QJSValuePrivate::asReturnedValue(), engine, QJSValuePrivate::engine(), QJSValuePrivate::fromReturnedValue(), and o.

Referenced by QQuickColorSpaceValueType::create(), QQuickFontValueType::create(), and setFontProperty().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ property() [2/2]

QJSValue QJSValue::property ( quint32 arrayIndex) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.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();
#define qDebug
[1]
Definition qlogging.h:164
QLatin1StringView QLatin1String
Definition qstringfwd.h:31

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:

\list

  • Is easier to use (can use an integer directly)
  • Is faster (no conversion to integer) \endlist

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

Definition at line 1120 of file qjsvalue.cpp.

References QJSValue(), QJSValuePrivate::asReturnedValue(), engine, QJSValuePrivate::engine(), QJSValuePrivate::fromReturnedValue(), and o.

+ Here is the call graph for this function:

◆ prototype()

QJSValue QJSValue::prototype ( ) const

If this QJSValue is an object, returns the internal prototype ({proto} property) of this object; otherwise returns an undefined QJSValue.

See also
setPrototype(), isObject()

Definition at line 841 of file qjsvalue.cpp.

References QJSValue(), engine, QJSValuePrivate::engine(), QJSValuePrivate::fromReturnedValue(), NullValue, and o.

Referenced by setPrototype().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setProperty() [1/2]

void QJSValue::setProperty ( const QString & name,
const QJSValue & value )

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.

To modify array elements, use the \l {QJSValue::}{setProperty(quint32 arrayIndex, const QJSValue &value)} overload instead.

See also
property(), deleteProperty()

Definition at line 1152 of file qjsvalue.cpp.

References QJSValuePrivate::asReturnedValue(), QJSValuePrivate::checkEngine(), QJSValuePrivate::convertToReturnedValue(), engine, QJSValuePrivate::engine(), o, and qWarning.

Referenced by QQuickText::fontInfo(), and main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setProperty() [2/2]

void QJSValue::setProperty ( quint32 arrayIndex,
const QJSValue & value )

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Sets the property at the given arrayIndex to the given 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);
EGLOutputLayerEXT EGLint EGLAttrib value
[5]

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:

\list

  • Is easier to use (can use an integer directly)
  • Is faster (no conversion to integer) \endlist

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

See also
{QJSValue::}{property(quint32 arrayIndex)}, {Working With Arrays}

Definition at line 1206 of file qjsvalue.cpp.

References QJSValuePrivate::asReturnedValue(), QJSValuePrivate::checkEngine(), QJSValuePrivate::convertToReturnedValue(), engine, QJSValuePrivate::engine(), QV4::PropertyKey::fromArrayIndex(), o, and qWarning.

+ Here is the call graph for this function:

◆ setPrototype()

void QJSValue::setPrototype ( const QJSValue & prototype)

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

See also
prototype(), isObject()

Definition at line 868 of file qjsvalue.cpp.

References QJSValuePrivate::asReturnedValue(), engine, QJSValuePrivate::engine(), QV4::Value::fromReturnedValue(), o, prototype(), and qWarning.

+ Here is the call graph for this function:

◆ strictlyEquals()

bool QJSValue::strictlyEquals ( const QJSValue & other) const

Returns true if this QJSValue is equal to other using strict comparison (no conversion), otherwise returns false.

The comparison follows the behavior described in \l{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:

\table \header

  • Type
  • Result \row
  • Undefined
  • true \row
  • Null
  • true \row
  • Boolean
  • true if both values are true, false otherwise \row
  • Number
  • false if either value is NaN (Not-a-Number); true if values are equal, false otherwise \row
  • String
  • true if both values are exactly the same sequence of characters, false otherwise \row
  • Object
  • true if both values refer to the same object, false otherwise \endtable
See also
equals()

Definition at line 1035 of file qjsvalue.cpp.

References QJSValuePrivate::asQString(), QJSValuePrivate::asReturnedValue(), other(), and QV4::RuntimeHelpers::strictEqual().

Referenced by QQuickTableViewPrivate::compareModel(), QQuickTableView::setColumnWidthProvider(), and QQuickTableView::setRowHeightProvider().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ toBool()

bool QJSValue::toBool ( ) const

Returns the boolean value of this QJSValue, using the conversion rules described in \l{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()

Definition at line 546 of file qjsvalue.cpp.

References QJSValuePrivate::asQString(), and QV4::Value::toBoolean().

Referenced by QQuickFontValueType::create().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ toDateTime()

QDateTime QJSValue::toDateTime ( ) const

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

Definition at line 1358 of file qjsvalue.cpp.

References date.

◆ toInt()

qint32 QJSValue::toInt ( ) const

Returns the signed 32-bit integer value of this QJSValue, using the conversion rules described in \l{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(), toUInt()

Definition at line 566 of file qjsvalue.cpp.

References QJSValuePrivate::asQString(), QV4::RuntimeHelpers::stringToNumber(), and QV4::Value::toInt32().

Referenced by QQuickSpinBoxPrivate::evaluateValueFromText(), and QQuickRectangle::setGradient().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ toNumber()

double QJSValue::toNumber ( ) const

Returns the number value of this QJSValue, as defined in \l{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(), toUInt()

Definition at line 526 of file qjsvalue.cpp.

References QJSValuePrivate::asQString(), QV4::RuntimeHelpers::stringToNumber(), and QV4::Value::toNumber().

Referenced by QQuickTableViewPrivate::getColumnWidth(), QQuickTableViewPrivate::getRowHeight(), and main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ toPrimitive()

QJSPrimitiveValue QJSValue::toPrimitive ( ) const

Converts the value to a QJSPrimitiveValue.

If the value holds a type supported by QJSPrimitiveValue, the value is copied. Otherwise the value is converted to a string, and the string is stored in QJSPrimitiveValue.

Note
Conversion of a managed value to a string can throw an exception. In particular, symbols cannot be coerced into strings, or a custom toString() method may throw. In this case the result is the undefined value and the engine carries an error after the conversion.

Definition at line 681 of file qjsvalue.cpp.

References QJSValuePrivate::asQString(), QJSValuePrivate::asReturnedValue(), QV4::ExecutionEngine::createPrimitive(), and QV4::Value::fromReturnedValue().

+ Here is the call graph for this function:

◆ toQMetaObject()

const QMetaObject * QJSValue::toQMetaObject ( ) const
Since
5.8

If this QJSValue is a QMetaObject, returns the QMetaObject pointer that the QJSValue represents; otherwise, returns \nullptr.

See also
isQMetaObject()

Definition at line 1337 of file qjsvalue.cpp.

References QJSValuePrivate::asReturnedValue(), engine, QJSValuePrivate::engine(), and wrapper().

+ Here is the call graph for this function:

◆ toQObject()

QObject * QJSValue::toQObject ( ) const

If this QJSValue is a QObject, returns the QObject pointer that the QJSValue represents; otherwise, returns \nullptr.

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

See also
isQObject()

Definition at line 1316 of file qjsvalue.cpp.

References QJSValuePrivate::asReturnedValue(), engine, QJSValuePrivate::engine(), and wrapper().

Referenced by QQuickRectangle::setGradient().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ toString()

QString QJSValue::toString ( ) const

Returns the string value of this QJSValue, as defined in \l{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()

Definition at line 494 of file qjsvalue.cpp.

References QJSValuePrivate::asQString(), QJSValuePrivate::asReturnedValue(), and QV4::Value::fromReturnedValue().

Referenced by QQuickSpinBoxPrivate::evaluateTextFromValue(), QuickTestResult::fuzzyCompare(), and QQuickRectangle::setGradient().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ toUInt()

quint32 QJSValue::toUInt ( ) const

Returns the unsigned 32-bit integer value of this QJSValue, using the conversion rules described in \l{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(), toInt()

Definition at line 586 of file qjsvalue.cpp.

References QJSValuePrivate::asQString(), QV4::RuntimeHelpers::stringToNumber(), and QV4::Value::toUInt32().

+ Here is the call graph for this function:

◆ toVariant() [1/2]

QVariant QJSValue::toVariant ( ) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns toVariant(ConvertJSObjects).

See also
isVariant()

Definition at line 601 of file qjsvalue.cpp.

References ConvertJSObjects, and toVariant().

Referenced by toVariant().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ toVariant() [2/2]

QVariant QJSValue::toVariant ( QJSValue::ObjectConversionBehavior behavior) const

Returns the QVariant value of this QJSValue, if it can be converted to a QVariant; otherwise returns an invalid QVariant.

Some JavaScript types and objects have native expressions in Qt. Those are converted to their native expressions. For example:

\table \header

For other types the behavior parameter is relevant. If ConvertJSObjects is given, a best effort but possibly lossy conversion is attempted. Generic JavaScript objects are converted to QVariantMap. JavaScript arrays are converted to QVariantList. Each property or element is converted to a QVariant, recursively; cyclic references are not followed. JavaScript function objects are dropped. If RetainJSObjects is given, the QJSValue is wrapped into a QVariant via QVariant::fromValue(). The resulting conversion is lossless but the internal structure of the objects is not immediately accessible.

See also
isVariant()

Definition at line 637 of file qjsvalue.cpp.

References QJSValuePrivate::asQString(), QJSValuePrivate::asReturnedValue(), QV4::Value::fromReturnedValue(), Q_ASSERT, RetainJSObjects, QV4::ExecutionEngine::toVariant(), and QV4::ExecutionEngine::toVariantLossy().

+ Here is the call graph for this function:

Friends And Related Symbol Documentation

◆ QJSValueList()

QJSValueList
related

This is a typedef for a QList<QJSValue>.

Definition at line 22 of file qjsvalue.h.

◆ QJSValuePrivate

friend class QJSValuePrivate
friend

Definition at line 135 of file qjsvalue.h.


The documentation for this class was generated from the following files: