diff options
author | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2018-06-06 11:17:13 +0200 |
---|---|---|
committer | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2018-07-30 12:35:13 +0000 |
commit | 19eca0600b907d9d31fa5c0f452d03aca0c8103d (patch) | |
tree | d166a4e55d60147802c9a13122792244f12119b8 /src/qml/util | |
parent | 4365c1ef47fc146b88f04b46ba40151e63471c55 (diff) |
QQmlAdaptorModel: prepare QQmlListProperty for delegate recycling
This patch will enable delegate item recycling for QQmlListProperty.
So if you e.g set "model: someItem.children" 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 a QQmlListProperty, 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 enables
delegate recycling for TableView.
Change-Id: I34161a998a4581798b692da69164ef29032d6e4f
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/qml/util')
-rw-r--r-- | src/qml/util/qqmladaptormodel.cpp | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/src/qml/util/qqmladaptormodel.cpp b/src/qml/util/qqmladaptormodel.cpp index 578d4f6a74..32bf43159a 100644 --- a/src/qml/util/qqmladaptormodel.cpp +++ b/src/qml/util/qqmladaptormodel.cpp @@ -676,7 +676,7 @@ class VDMObjectDelegateDataType; class QQmlDMObjectData : public QQmlDelegateModelItem, public QQmlAdaptorModelProxyInterface { Q_OBJECT - Q_PROPERTY(QObject *modelData READ modelData CONSTANT) + Q_PROPERTY(QObject *modelData READ modelData NOTIFY modelDataChanged) Q_INTERFACES(QQmlAdaptorModelProxyInterface) public: QQmlDMObjectData( @@ -685,10 +685,22 @@ public: int index, int row, int column, QObject *object); + void setModelData(QObject *modelData) + { + if (modelData == object) + return; + + object = modelData; + emit modelDataChanged(); + } + QObject *modelData() const { return object; } QObject *proxiedObject() override { return object; } QPointer<QObject> object; + +Q_SIGNALS: + void modelDataChanged(); }; class VDMObjectDelegateDataType : public QQmlRefCount, public QQmlAdaptorModel::Accessors @@ -769,6 +781,20 @@ public: { const_cast<VDMObjectDelegateDataType *>(this)->release(); } + + bool notify(const QQmlAdaptorModel &model, const QList<QQmlDelegateModelItem *> &items, int index, int count, const QVector<int> &) const override + { + for (auto modelItem : items) { + const int modelItemIndex = modelItem->index; + if (modelItemIndex < index || modelItemIndex >= index + count) + continue; + + auto objectModelItem = static_cast<QQmlDMObjectData *>(modelItem); + QObject *updatedModelData = qvariant_cast<QObject *>(model.list.at(objectModelItem->index)); + objectModelItem->setModelData(updatedModelData); + } + return true; + } }; class QQmlDMObjectDataMetaObject : public QAbstractDynamicMetaObject |