diff options
Diffstat (limited to 'src/qmlmodels/qqmllistmodel.cpp')
-rw-r--r-- | src/qmlmodels/qqmllistmodel.cpp | 62 |
1 files changed, 33 insertions, 29 deletions
diff --git a/src/qmlmodels/qqmllistmodel.cpp b/src/qmlmodels/qqmllistmodel.cpp index 14c8a6fd85..de7d97af2f 100644 --- a/src/qmlmodels/qqmllistmodel.cpp +++ b/src/qmlmodels/qqmllistmodel.cpp @@ -3,22 +3,25 @@ #include "qqmllistmodel_p_p.h" #include "qqmllistmodelworkeragent_p.h" -#include <private/qqmlopenmetaobject_p.h> -#include <private/qqmljsast_p.h> -#include <private/qqmljsengine_p.h> + #include <private/qjsvalue_p.h> #include <private/qqmlcustomparser_p.h> #include <private/qqmlengine_p.h> +#include <private/qqmljsast_p.h> +#include <private/qqmljsengine_p.h> +#include <private/qqmllistwrapper_p.h> #include <private/qqmlnotifier_p.h> +#include <private/qqmlopenmetaobject_p.h> -#include <private/qv4object_p.h> -#include <private/qv4dateobject_p.h> -#include <private/qv4urlobject_p.h> -#include <private/qv4objectiterator_p.h> #include <private/qv4alloca_p.h> +#include <private/qv4dateobject_p.h> #include <private/qv4lookup_p.h> +#include <private/qv4object_p.h> +#include <private/qv4objectiterator_p.h> #include <private/qv4qmlcontext_p.h> +#include <private/qv4sequenceobject_p.h> +#include <private/qv4urlobject_p.h> #include <qqmlcontext.h> #include <qqmlinfo.h> @@ -319,9 +322,12 @@ QObject *ListModel::getOrCreateModelObject(QQmlListModel *model, int elementInde void *memory = operator new(sizeof(QObject) + sizeof(QQmlData)); void *ddataMemory = ((char *)memory) + sizeof(QObject); e->m_objectCache = new (memory) QObject; - QQmlData *ddata = new (ddataMemory) QQmlData; - ddata->ownMemory = false; - QObjectPrivate::get(e->m_objectCache)->declarativeData = ddata; + + const QAbstractDeclarativeData *old = std::exchange( + QObjectPrivate::get(e->m_objectCache)->declarativeData, + new (ddataMemory) QQmlData(QQmlData::DoesNotOwnMemory)); + Q_ASSERT(!old); // QObject should really not manipulate QQmlData + (void)new ModelNodeMetaObject(e->m_objectCache, model, elementIndex); } return e->m_objectCache; @@ -678,18 +684,11 @@ void ListModel::set(int elementIndex, QV4::Object *object, ListModel::SetElement e->setDoublePropertyFast(r, propertyValue->asDouble()); } } else if (QV4::ArrayObject *a = propertyValue->as<QV4::ArrayObject>()) { - const ListLayout::Role &r = m_layout->getRoleOrCreate(propertyName, ListLayout::Role::List); - if (r.type == ListLayout::Role::List) { - ListModel *subModel = new ListModel(r.subLayout, nullptr); - - int arrayLength = a->getLength(); - for (int j=0 ; j < arrayLength ; ++j) { - o = a->get(j); - subModel->append(o); - } - - e->setListPropertyFast(r, subModel); - } + setArrayLike(&o, propertyName, e, a); + } else if (QV4::Sequence *s = propertyValue->as<QV4::Sequence>()) { + setArrayLike(&o, propertyName, e, s); + } else if (QV4::QmlListWrapper *l = propertyValue->as<QV4::QmlListWrapper>()) { + setArrayLike(&o, propertyName, e, l); } else if (propertyValue->isBoolean()) { const ListLayout::Role &r = m_layout->getRoleOrCreate(propertyName, ListLayout::Role::Bool); if (r.type == ListLayout::Role::Bool) { @@ -1913,6 +1912,7 @@ void DynamicRoleModelNodeMetaObject::propertyWritten(int index) /*! \qmltype ListModel \instantiates QQmlListModel + \inherits AbstractListModel \inqmlmodule QtQml.Models \ingroup qtquick-models \brief Defines a free-form list data source. @@ -1930,6 +1930,10 @@ void DynamicRoleModelNodeMetaObject::propertyWritten(int index) Elements can be manipulated via the model using the setProperty() method, which allows the roles of the specified element to be set and changed. + ListModel inherits from \l{QAbstractListModel} and provides its \l{Q_INVOKABLE} + methods. You can, for example use \l{QAbstractItemModel::index} to retrieve a + \l{QModelIndex} for a row and column. + \section1 Example Usage The following example shows a ListModel containing three elements, with the roles @@ -1978,7 +1982,7 @@ void DynamicRoleModelNodeMetaObject::propertyWritten(int index) \section1 Using Threaded List Models with WorkerScript - ListModel can be used together with WorkerScript access a list model + ListModel can be used together with WorkerScript to access a list model from multiple threads. This is useful if list modifications are synchronous and take some time: the list operations can be moved to a different thread to avoid blocking of the main GUI thread. @@ -1986,11 +1990,11 @@ void DynamicRoleModelNodeMetaObject::propertyWritten(int index) Here is an example that uses WorkerScript to periodically append the current time to a list model: - \snippet ../../examples/quick/threading/threadedlistmodel/timedisplay.qml 0 + \snippet qml/listmodel/WorkerScript.qml 0 The included file, \tt dataloader.mjs, looks like this: - \snippet ../../examples/quick/threading/threadedlistmodel/dataloader.mjs 0 + \snippet qml/listmodel/dataloader.mjs 0 The timer in the main example sends messages to the worker script by calling \l WorkerScript::sendMessage(). When this message is received, @@ -2001,7 +2005,7 @@ void DynamicRoleModelNodeMetaObject::propertyWritten(int index) You must call sync() or else the changes made to the list from that thread will not be reflected in the list model in the main thread. - \sa {qml-data-models}{Data Models}, {Qt Quick Examples - Threading}, {Qt QML} + \sa {qml-data-models}{Data Models}, {Qt Qml} */ QQmlListModel::QQmlListModel(QObject *parent) @@ -2381,7 +2385,7 @@ void QQmlListModel::clear() \sa clear() */ -void QQmlListModel::remove(QQmlV4Function *args) +void QQmlListModel::remove(QQmlV4FunctionPtr args) { int argLength = args->length(); @@ -2469,7 +2473,7 @@ void QQmlListModel::updateTranslations() \sa set(), append() */ -void QQmlListModel::insert(QQmlV4Function *args) +void QQmlListModel::insert(QQmlV4FunctionPtr args) { if (args->length() == 2) { QV4::Scope scope(args->v4engine()); @@ -2585,7 +2589,7 @@ void QQmlListModel::move(int from, int to, int n) \sa set(), remove() */ -void QQmlListModel::append(QQmlV4Function *args) +void QQmlListModel::append(QQmlV4FunctionPtr args) { if (args->length() == 1) { QV4::Scope scope(args->v4engine()); |