diff --git a/src/qmlmodels/qqmldelegatemodel.cpp b/src/qmlmodels/qqmldelegatemodel.cpp index 444379818a..c6e06ac6f4 100644 --- a/src/qmlmodels/qqmldelegatemodel.cpp +++ b/src/qmlmodels/qqmldelegatemodel.cpp @@ -1617,7 +1617,7 @@ void QQmlDelegateModelPrivate::itemsRemoved( emitDestroyingItem(object); cacheItem->scriptRef -= 1; } - if (!cacheItem->isReferenced()) { + if (!cacheItem->isReferenced() && !remove.inGroup(Compositor::Persisted)) { m_compositor.clearFlags(Compositor::Cache, cacheIndex, 1, Compositor::CacheFlag); m_cache.removeAt(cacheIndex); delete cacheItem; diff --git a/tests/auto/qml/qqmldelegatemodel/data/persistedItemsCache.qml b/tests/auto/qml/qqmldelegatemodel/data/persistedItemsCache.qml new file mode 100644 index 0000000000..5ae2038e1f --- /dev/null +++ b/tests/auto/qml/qqmldelegatemodel/data/persistedItemsCache.qml @@ -0,0 +1,62 @@ +import QtQuick +import QtQuick.Window +import QtQml.Models + +Window { + id: win + visible: true + width: 640 + height: 480 + property int destroyCount : 0; + property int createCount : 0; + property alias testListModel: mdl + + DelegateModel { + id: visualModel + model: ListModel { + id: mdl + ListElement { + name: "a" + hidden: false + } + ListElement { + name: "b" + hidden: true + } + ListElement { + name: "c" + hidden: false + } + } + + filterOnGroup: "selected" + + groups: [ + DelegateModelGroup { + name: "selected" + includeByDefault: true + } + ] + + delegate: Text { + visible: DelegateModel.inSelected + property var idx + Component.onCompleted: { + ++createCount + idx = index + DelegateModel.inPersistedItems = true + DelegateModel.inSelected = !model.hidden + } + Component.onDestruction: ++destroyCount + text: model.name + } + } + + ListView { + id: listView + model: visualModel + anchors.fill: parent + focus: true + } + +} diff --git a/tests/auto/qml/qqmldelegatemodel/tst_qqmldelegatemodel.cpp b/tests/auto/qml/qqmldelegatemodel/tst_qqmldelegatemodel.cpp index 8b433d346c..4169690ed0 100644 --- a/tests/auto/qml/qqmldelegatemodel/tst_qqmldelegatemodel.cpp +++ b/tests/auto/qml/qqmldelegatemodel/tst_qqmldelegatemodel.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -28,6 +29,7 @@ private slots: void nestedDelegates(); void typedModelData(); void deleteRace(); + void persistedItemsStayInCache(); }; class AbstractItemModel : public QAbstractItemModel @@ -289,6 +291,21 @@ void tst_QQmlDelegateModel::deleteRace() QTRY_COMPARE(o->property("count").toInt(), 0); } +void tst_QQmlDelegateModel::persistedItemsStayInCache() +{ + QQmlEngine engine; + QQmlComponent component(&engine, testFileUrl("persistedItemsCache.qml")); + QVERIFY2(component.isReady(), qPrintable(component.errorString())); + std::unique_ptr object(component.create()); + QVERIFY(object); + const QVariant properyListModel = object->property("testListModel"); + QQmlListModel *listModel = qvariant_cast(properyListModel); + QVERIFY(listModel); + QTRY_COMPARE(object->property("createCount").toInt(), 3); + listModel->clear(); + QTRY_COMPARE(object->property("destroyCount").toInt(), 3); +} + QTEST_MAIN(tst_QQmlDelegateModel) #include "tst_qqmldelegatemodel.moc"