From cadae7a431a831a30963e1973bef9dfc8896061d Mon Sep 17 00:00:00 2001 From: Richard Moe Gustavsen Date: Wed, 6 Jun 2018 10:57:37 +0200 Subject: QQmlAdaptorModel: prepare js list models for recycling support This patch will enable delegate item recycling for js list models. So if you e.g set "model: [1, 2, 3, 4]" on a TableView, the delegates can be recycled. The patch will override the notify function. This function is called from QQmlDelegateModel whenever the modelData that the delegate item depends on, changes. This again is needed to trigger all the modelData bindings in the item to update. Note that this function will only be used for recycling items. The model classes don't listen for changes done to js list models, meaning that the function will otherwise never be called (which is why it was never implemented, I guess). Auto testing this will be included with the patches that implements delegate recycling for TableView. Change-Id: I49185bdbaaacc3ccbd94c99cc66d9a1998452f68 Reviewed-by: Mitch Curtis --- src/qml/util/qqmladaptormodel.cpp | 23 ++++++++++++++++++---- .../tst_qquickvisualdatamodel.cpp | 2 +- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/qml/util/qqmladaptormodel.cpp b/src/qml/util/qqmladaptormodel.cpp index 32bf43159a..e460c376da 100644 --- a/src/qml/util/qqmladaptormodel.cpp +++ b/src/qml/util/qqmladaptormodel.cpp @@ -566,10 +566,11 @@ public: void setModelData(const QVariant &data) { - if (index == -1 && data != cachedData) { - cachedData = data; - emit modelDataChanged(); - } + if (data == cachedData) + return; + + cachedData = data; + emit modelDataChanged(); } static QV4::ReturnedValue get_modelData(const QV4::FunctionObject *b, const QV4::Value *thisObject, const QV4::Value *, int) @@ -666,6 +667,20 @@ public: index, row, column, index >= 0 && index < model.list.count() ? model.list.at(index) : QVariant()); } + + bool notify(const QQmlAdaptorModel &model, const QList &items, int index, int count, const QVector &) const override + { + for (auto modelItem : items) { + const int modelItemIndex = modelItem->index; + if (modelItemIndex < index || modelItemIndex >= index + count) + continue; + + auto listModelItem = static_cast(modelItem); + QVariant updatedModelData = model.list.at(listModelItem->index); + listModelItem->setModelData(updatedModelData); + } + return true; + } }; //----------------------------------------------------------------- diff --git a/tests/auto/quick/qquickvisualdatamodel/tst_qquickvisualdatamodel.cpp b/tests/auto/quick/qquickvisualdatamodel/tst_qquickvisualdatamodel.cpp index da227b871b..6498a80c79 100644 --- a/tests/auto/quick/qquickvisualdatamodel/tst_qquickvisualdatamodel.cpp +++ b/tests/auto/quick/qquickvisualdatamodel/tst_qquickvisualdatamodel.cpp @@ -2996,7 +2996,7 @@ void tst_qquickvisualdatamodel::insert_data() "items.get(2).model.modelData = \"seven\"; }") << 4 << 5 << 0 << true << false << false << false << false << QString("modelData") - << (QStringList() << "eight" << "one" << "two" << "three" << "four"); + << (QStringList() << "eight" << "one" << "seven" << "three" << "four"); QTest::newRow("StringList.create prepend modelData") << stringListSource[i] -- cgit v1.2.3