| qjsengine.cpp |
|
48181 |
- |
| qjsengine.h |
|
15002 |
- |
| qjsengine_p.h |
|
1719 |
- |
| qjslist.cpp |
!
\class QJSListIndexClamp
\internal
|
335 |
- |
| qjslist.h |
|
10713 |
- |
| qjsmanagedvalue.cpp |
!
\class QJSManagedValue
\inmodule QtQml
\since 6.1
\inmodule QtQml
\brief QJSManagedValue represents a value on the JavaScript heap belonging to a QJSEngine.
The QJSManagedValue class allows interaction with JavaScript values in most
ways you can interact with them from JavaScript itself. You can get and set
properties and prototypes, and you can access arrays. Additionally, you can
transform the value into the Qt counterparts of JavaScript objects. For
example, a Url object may be transformed into a QUrl.
A QJSManagedValue is always bound to a particular QJSEngine. You cannot use
it independently. This means that you cannot have a QJSManagedValue from one
engine be a property or a proptotype of a QJSManagedValue from a different
engine.
In contrast to QJSValue, almost all values held by QJSManagedValue live on
the JavaScript heap. There is no inline or unmanaged storage. Therefore, you
can get the prototype of a primitive value, and you can get the \c length
property of a string.
Only default-constructed or moved-from QJSManagedValues do not hold a value
on the JavaScript heap. They represent \c undefined, which doesn't have any
properties or prototypes.
Also in contrast to QJSValue, QJSManagedValue does not catch any JavaScript
exceptions. If an operation on a QJSManagedValue causes an error, it will
generally return an \c undefined value and QJSEngine::hasError() will return
\c true afterwards. You can then catch the exception using
QJSEngine::catchError(), or pass it up the stack, at your own discretion.
\note As the reference to the value on the JavaScript heap has to be freed
on destruction, you cannot move a QJSManagedValue to a different thread.
The destruction would take place in the new thread, which would create a race
condition with the garbage collector on the original thread. This also means
that you cannot hold a QJSManagedValue beyond the lifespan of its engine.
The recommended way of working with a QJSManagedValue is creating it
on the stack, possibly by moving a QJSValue and adding an engine, then
performing the necessary operations on it, and finally moving it back into a
QJSValue for storage. Moving between QJSManagedValue and QJSValue is fast.
|
36611 |
- |
| qjsmanagedvalue.h |
|
3930 |
- |
| qjsmanagedvalue_p.h |
|
1093 |
- |
| qjsprimitivevalue.cpp |
!
\since 6.1
\class QJSPrimitiveUndefined
\inmodule QtQml
\brief An empty marker type to signify the JavaScript Undefined type and its single value.
\inmodule QtQml
|
9681 |
- |
| qjsprimitivevalue.h |
|
34262 |
- |
| qjsvalue.cpp |
!
\since 5.0
\class QJSValue
\brief The QJSValue class acts as a container for Qt/JavaScript data types.
\ingroup qtjavascript
\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.
\snippet code/src_script_qjsvalue.cpp 0
If you want to iterate over the properties of a script object, use
the QJSValueIterator class.
Object values have an internal \c{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 \c{for-in} statement in
script code, or QJSValueIterator in C++.
\sa QJSEngine, QJSValueIterator
\section1 Working With Arrays
To create an array using QJSValue, use \l QJSEngine::newArray():
\code
// Assumes that this class was declared in QML.
QJSValue jsArray = engine->newArray(3);
\endcode
To set individual elements in the array, use
the \l {QJSValue::}{setProperty(quint32 arrayIndex, const QJSValue &value)}
overload. For example, to fill the array above with integers:
\code
for (int i = 0; i < 3; ++i) {
jsArray.setProperty(i, QRandomGenerator::global().generate());
}
\endcode
To determine the length of the array, access the \c "length" property.
To access array elements, use the
\l {QJSValue::}{property(quint32 arrayIndex)} overload. The following code
reads the array we created above back into a list:
\code
QList<int> integers;
const int length = jsArray.property("length").toInt();
for (int i = 0; i < length; ++i) {
integers.append(jsArray.property(i).toInt());
}
\endcode
\section2 Converting to JSON
It's possible to convert a QJSValue to a JSON type. For example,
to convert to an array, use \l QJSEngine::fromScriptValue():
\code
const QJsonValue jsonValue = engine.fromScriptValue<QJsonValue>(jsValue);
const QJsonArray jsonArray = jsonValue.toArray();
\endcode
|
48421 |
- |
| qjsvalue.h |
|
3895 |
- |
| qjsvalue_p.h |
|
11458 |
- |
| qjsvalueiterator.cpp |
!
\class QJSValueIterator
\brief The QJSValueIterator class provides a Java-style iterator for QJSValue.
\ingroup qtjavascript
\inmodule QtQml
The QJSValueIterator constructor takes a QJSValue as
argument. After construction, the iterator is located at the very
beginning of the sequence of properties. Here's how to iterate over
all the properties of a QJSValue:
\snippet code/src_script_qjsvalueiterator.cpp 0
The next() advances the iterator. The name() and value()
functions return the name and value of the last item that was
jumped over.
Note that QJSValueIterator only iterates over the QJSValue's
own properties; i.e. it does not follow the prototype chain. You can
use a loop like this to follow the prototype chain:
\snippet code/src_script_qjsvalueiterator.cpp 1
\sa QJSValue::property()
|
4843 |
- |
| qjsvalueiterator.h |
|
852 |
- |
| qjsvalueiterator_p.h |
|
995 |
- |