diff options
author | Liang Qi <liang.qi@qt.io> | 2018-02-18 13:37:02 +0100 |
---|---|---|
committer | Liang Qi <liang.qi@qt.io> | 2018-02-18 13:37:02 +0100 |
commit | ba05397ad723fa19125eb5ca2f91c7f437b21484 (patch) | |
tree | e236df3317828f02ec1e8f28d87125bcc286b2d9 /src/qml | |
parent | b2c71d6518143b3d2a9bd3aef1d72ee5929891fc (diff) | |
parent | 0e64bd96fbf26855a65e38847083dbd2c6bee4ac (diff) |
Merge remote-tracking branch 'origin/5.9' into 5.11
Conflicts:
.qmake.conf
tests/auto/qml/qqmlcontext/tst_qqmlcontext.cpp
Change-Id: I7feb9772fc35066f56b7c073482b53ca8c86c70b
Diffstat (limited to 'src/qml')
-rw-r--r-- | src/qml/jsapi/qjsvalue.cpp | 86 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4qmlcontext.cpp | 4 | ||||
-rw-r--r-- | src/qml/qml/qqmlcontext.cpp | 2 | ||||
-rw-r--r-- | src/qml/qml/qqmltypeloader.cpp | 1 | ||||
-rw-r--r-- | src/qml/types/qqmlobjectmodel_p.h | 1 |
5 files changed, 87 insertions, 7 deletions
diff --git a/src/qml/jsapi/qjsvalue.cpp b/src/qml/jsapi/qjsvalue.cpp index adfa98bd46..c4090ac482 100644 --- a/src/qml/jsapi/qjsvalue.cpp +++ b/src/qml/jsapi/qjsvalue.cpp @@ -67,7 +67,7 @@ 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 type. Additionally, built-in + 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 @@ -108,6 +108,38 @@ 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 + QVector<int> integers; + const int length = jsArray.property("length").toInt(); + for (int i = 0; i < length; ++i) { + integers.append(jsArray.property(i).toInt()); + } + \endcode */ /*! @@ -1008,6 +1040,10 @@ bool QJSValue::strictlyEquals(const QJSValue& other) const occurred, property() returns the value that was thrown (typically an \c{Error} object). + To access array elements, use the + \l {QJSValue::}{setProperty(quint32 arrayIndex, const QJSValue &value)} + overload instead. + \sa setProperty(), hasProperty(), QJSValueIterator */ QJSValue QJSValue::property(const QString& name) const @@ -1039,8 +1075,25 @@ QJSValue QJSValue::property(const QString& name) const Returns the property at the given \a arrayIndex. - This function is provided for convenience and performance when - working with array objects. + It is possible to access elements in an array in two ways. The first is to + use the array index as the property name: + + \code + qDebug() << jsValueArray.property(QLatin1String("4")).toString(); + \endcode + + The second is to use the overload that takes an index: + + \code + qDebug() << jsValueArray.property(4).toString(); + \endcode + + Both of these approaches achieve the same result, except that the latter: + + \list + \li Is easier to use (can use an integer directly) + \li 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 \a @@ -1072,6 +1125,10 @@ QJSValue QJSValue::property(quint32 arrayIndex) const If this QJSValue does not already have a property with name \a name, a new property is created. + To modify array elements, use the + \l {QJSValue::}{setProperty(quint32 arrayIndex, const QJSValue &value)} + overload instead. + \sa property(), deleteProperty() */ void QJSValue::setProperty(const QString& name, const QJSValue& value) @@ -1109,12 +1166,31 @@ void QJSValue::setProperty(const QString& name, const QJSValue& value) Sets the property at the given \a arrayIndex to the given \a value. - This function is provided for convenience and performance when - working with array objects. + It is possible to modify elements in an array in two ways. The first is to + use the array index as the property name: + + \code + jsValueArray.setProperty(QLatin1String("4"), value); + \endcode + + The second is to use the overload that takes an index: + + \code + jsValueArray.setProperty(4, value); + \endcode + + Both of these approaches achieve the same result, except that the latter: + + \list + \li Is easier to use (can use an integer directly) + \li 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 \a arrayIndex. + + \sa {QJSValue::}{property(quint32 arrayIndex)}, {Working With Arrays} */ void QJSValue::setProperty(quint32 arrayIndex, const QJSValue& value) { diff --git a/src/qml/jsruntime/qv4qmlcontext.cpp b/src/qml/jsruntime/qv4qmlcontext.cpp index 9c33966cf7..cf4ef68bc0 100644 --- a/src/qml/jsruntime/qv4qmlcontext.cpp +++ b/src/qml/jsruntime/qv4qmlcontext.cpp @@ -138,7 +138,9 @@ ReturnedValue QQmlContextWrapper::get(const Managed *m, String *name, bool *hasP *hasProperty = true; if (r.scriptIndex != -1) { QV4::ScopedObject scripts(scope, context->importedScripts.valueRef()); - return scripts->getIndexed(r.scriptIndex); + if (scripts) + return scripts->getIndexed(r.scriptIndex); + return QV4::Encode::null(); } else if (r.type.isValid()) { return QQmlTypeWrapper::create(v4, scopeObject, r.type); } else if (r.importNamespace) { diff --git a/src/qml/qml/qqmlcontext.cpp b/src/qml/qml/qqmlcontext.cpp index de43108312..0b0132fab7 100644 --- a/src/qml/qml/qqmlcontext.cpp +++ b/src/qml/qml/qqmlcontext.cpp @@ -587,6 +587,8 @@ void QQmlContextData::invalidate() prevChild = 0; } + importedScripts.clear(); + engine = 0; parent = 0; } diff --git a/src/qml/qml/qqmltypeloader.cpp b/src/qml/qml/qqmltypeloader.cpp index 0be83f6ba6..e5682df761 100644 --- a/src/qml/qml/qqmltypeloader.cpp +++ b/src/qml/qml/qqmltypeloader.cpp @@ -2916,6 +2916,7 @@ QV4::ReturnedValue QQmlScriptData::scriptValueForContext(QQmlContextData *parent m_program->qmlContext.set(scope.engine, qmlContext); m_program->run(); + m_program->qmlContext.clear(); if (scope.engine->hasException) { QQmlError error = scope.engine->catchExceptionAsQmlError(); if (error.isValid()) diff --git a/src/qml/types/qqmlobjectmodel_p.h b/src/qml/types/qqmlobjectmodel_p.h index c98fe13a6b..b3cf45ca62 100644 --- a/src/qml/types/qqmlobjectmodel_p.h +++ b/src/qml/types/qqmlobjectmodel_p.h @@ -75,7 +75,6 @@ public: virtual int count() const = 0; virtual bool isValid() const = 0; - QObject *object(int index, bool async) { return object(index, async ? QQmlIncubator::Asynchronous : QQmlIncubator::AsynchronousIfNested); } virtual QObject *object(int index, QQmlIncubator::IncubationMode incubationMode = QQmlIncubator::AsynchronousIfNested) = 0; virtual ReleaseFlags release(QObject *object) = 0; virtual void cancel(int) {} |