6.0.0-rc4Source

QJSValue Class

The QJSValue class holds a value of a JavaScript engine. More…

Header#include <QJSValue>
CMaketarget_link_libraries(app PRIVATE Nt::Qml)
ModuleNt Qml C++ Classes

List of all members, including inherited members

Public Types

Public Functions

QJSValue(SpecialValue value = UndefinedValue)
QJSValue(bool value)
QJSValue(int value)
QJSValue(uint value)
QJSValue(double value)
QJSValue(const QString &value)
QJSValue(QLatin1String value)
QJSValue(const char *str)
QJSValue(const QJSValue &other)
QJSValue(QJSValue &&other)
QJSValue &operator=(const QJSValue &other)
QJSValue &operator=(QJSValue &&other)
~QJSValue()
voidswap(QJSValue &other)
boolisBool() const
boolisNumber() const
boolisNull() const
boolisString() const
boolisUndefined() const
boolisVariant() const
boolisQObject() const
boolisObject() const
boolisDate() const
boolisRegExp() const
boolisArray() const
boolisError() const
boolisUrl() const
boolisCallable() const
QStringtoString() const
doubletoNumber() const
qint32toInt() const
quint32toUInt() const
booltoBool() const
QVarianttoVariant() const
QVarianttoVariant(ObjectConversionBehavior behavior) const
QObject *toQObject() const
QDateTimetoDateTime() const
boolequals(const QJSValue &other) const
boolstrictlyEquals(const QJSValue &other) const
QJSValueprototype() const
voidsetPrototype(const QJSValue &prototype)
QJSValueproperty(const QString &name) const
voidsetProperty(const QString &name, const QJSValue &value)
boolhasProperty(const QString &name) const
boolhasOwnProperty(const QString &name) const
QJSValueproperty(quint32 arrayIndex) const
voidsetProperty(quint32 arrayIndex, const QJSValue &value)
booldeleteProperty(const QString &name)
QJSValuecall(const QJSValueList &args = QJSValueList()) const
QJSValuecallWithInstance(const QJSValue &instance, const QJSValueList &args = QJSValueList()) const
QJSValuecallAsConstructor(const QJSValueList &args = QJSValueList()) const
ErrorTypeerrorType() const

Detailed Description

A QJSValue is a primitive (undefined, null, a boolean, a number or a string) or a reference to an object of a QJSEngine. Copies refer to the same object, and keep it alive.

QJSValue point = engine.newObject();
point.setProperty("x", 3);
point.setProperty("y", 4);
QJSValue length = engine.evaluate("(function(p) { return Math.hypot(p.x, p.y); })");
qDebug() << length.call({point}).toNumber();   // 5

Values made without an engine, such as QJSValue(42), are primitives; they can be passed to any engine. Objects belong to the engine that made them; after the engine is destroyed they read as undefined.

Calling a function that throws returns the error: check isError(). Arguments are passed as a QJSValueList, a QList of QJSValue.

See also QJSEngine.

Differences from Qt

There is no toPrimitive(), toQMetaObject() or isQMetaObject(). toVariant() with RetainJSObjects converts objects as ConvertJSObjects does.

Member Type Documentation

enum QJSValue::ErrorType

The type of an error object.

ConstantDescription
QJSValue::NoErrorNot an error.
QJSValue::GenericErrorAn Error.
QJSValue::EvalErrorAn EvalError.
QJSValue::RangeErrorA RangeError.
QJSValue::ReferenceErrorA ReferenceError.
QJSValue::SyntaxErrorA SyntaxError.
QJSValue::TypeErrorA TypeError.
QJSValue::URIErrorA URIError.

enum QJSValue::ObjectConversionBehavior

How toVariant() converts objects.

ConstantDescription
QJSValue::ConvertJSObjectsArrays become QVariantList and objects QVariantMap.
QJSValue::RetainJSObjectsThe same in Nt.

enum QJSValue::SpecialValue

ConstantDescription
QJSValue::NullValueA null value.
QJSValue::UndefinedValueAn undefined value.

Member Function Documentation

QJSValue::QJSValue(SpecialValue value = UndefinedValue)

Constructs value: undefined or null.

QJSValue::QJSValue(bool value)

Constructs the boolean value.

QJSValue::QJSValue(int value)

Constructs the number value.

QJSValue::QJSValue(uint value)

Constructs the number value.

QJSValue::QJSValue(double value)

Constructs the number value.

QJSValue::QJSValue(const QString &value)

Constructs the string value.

QJSValue::QJSValue(QLatin1String value)

Constructs the string value.

QJSValue::QJSValue(const char *str)

Constructs the string str, read as UTF-8.

QJSValue::QJSValue(const QJSValue &other)

Constructs a copy of other, which refers to the same object.

QJSValue::QJSValue(QJSValue &&other)

Constructs a value by moving other.

QJSValue::~QJSValue()

Destroys the value. The object it refers to may be collected once nothing else refers to it.

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

Calls the function with args and this undefined, and returns its result, or the error it throws.

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

Calls the function as a constructor, as new does, with args, and returns the new object, or the error it throws.

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

Calls the function with args and instance as this, and returns its result, or the error it throws.

bool QJSValue::deleteProperty(const QString &name)

Deletes the property name of the object. Returns true on success.

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

Returns true if the value equals other with JavaScript's == operator; otherwise returns false.

ErrorType QJSValue::errorType() const

Returns the type of the error, or NoError if the value is not an error.

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

Returns true if the object itself has the property name; otherwise returns false.

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

Returns true if the object or its prototypes have the property name; otherwise returns false.

bool QJSValue::isArray() const

Returns true if the value is an array; otherwise returns false.

bool QJSValue::isBool() const

Returns true if the value is a boolean; otherwise returns false.

bool QJSValue::isCallable() const

Returns true if the value is a function; otherwise returns false.

bool QJSValue::isDate() const

Returns true if the value is a Date; otherwise returns false.

bool QJSValue::isError() const

Returns true if the value is an error; otherwise returns false.

bool QJSValue::isNull() const

Returns true if the value is null; otherwise returns false.

bool QJSValue::isNumber() const

Returns true if the value is a number; otherwise returns false.

bool QJSValue::isObject() const

Returns true if the value is an object, functions and arrays included; otherwise returns false.

bool QJSValue::isQObject() const

Returns true if the value is a QObject; otherwise returns false.

See also toQObject().

bool QJSValue::isRegExp() const

Returns true if the value is a RegExp; otherwise returns false.

bool QJSValue::isString() const

Returns true if the value is a string; otherwise returns false.

bool QJSValue::isUndefined() const

Returns true if the value is undefined; otherwise returns false.

bool QJSValue::isUrl() const

Returns true if the value is a URL; otherwise returns false. URLs are strings in Nt, so this is always false.

bool QJSValue::isVariant() const

Returns true if the value holds a C++ value that has no JavaScript form; otherwise returns false.

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

Makes this value refer to what other refers to.

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

Moves other into this value.

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

Returns the property name of the object, or undefined. A getter that throws gives the error.

QJSValue QJSValue::property(quint32 arrayIndex) const

This is an overloaded function.

Returns the element arrayIndex of the object.

QJSValue QJSValue::prototype() const

Returns the prototype of the object, or null.

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

Sets the property name of the object to value.

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

This is an overloaded function.

Sets the element arrayIndex of the object to value.

void QJSValue::setPrototype(const QJSValue &prototype)

Sets the prototype of the object to prototype, an object or null. A prototype that would make a cycle is ignored.

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

Returns true if the value equals other with JavaScript's === operator; otherwise returns false.

void QJSValue::swap(QJSValue &other)

Swaps this value with other.

bool QJSValue::toBool() const

Returns the value converted to a boolean, as JavaScript's Boolean(value) does.

QDateTime QJSValue::toDateTime() const

Returns the date the value holds, or an invalid QDateTime if it is not a Date.

qint32 QJSValue::toInt() const

Returns the value converted to a 32-bit integer, as JavaScript's value | 0 does.

double QJSValue::toNumber() const

Returns the value converted to a number, as JavaScript's Number(value) does.

QObject * QJSValue::toQObject() const

Returns the QObject the value refers to, or nullptr if it is not a QObject or the object was deleted.

QString QJSValue::toString() const

Returns the value converted to a string, as JavaScript's String(value) does. An error while converting gives the text of the error.

quint32 QJSValue::toUInt() const

Returns the value converted to a 32-bit unsigned integer, as JavaScript's value >>> 0 does.

QVariant QJSValue::toVariant() const

Returns the value as a QVariant: booleans, numbers (as double) and strings as such, arrays as QVariantList, dates as QDateTime, QObjects as QObject *, other objects as QVariantMap, and null as a QVariant holding nullptr.

QVariant QJSValue::toVariant(ObjectConversionBehavior behavior) const

This is an overloaded function.

Returns the value as a QVariant; behavior is not used.