diff options
author | Maximilian Goldstein <max.goldstein@qt.io> | 2021-01-08 11:58:55 +0100 |
---|---|---|
committer | Maximilian Goldstein <max.goldstein@qt.io> | 2021-01-11 15:22:00 +0100 |
commit | 8a3ede5ba1daf2d9314d71db972175b2791456c0 (patch) | |
tree | 2d48e36b27779911a5b41c24cb9142bfa859a4a5 | |
parent | 30136b1a82bfc54459741483e76f2f70c937dedd (diff) |
Fix warning and assert when removing items from groups
When removing items from groups actively shown by a model filter a warning
and assert was triggered in the past. This change fixes this behavior.
Fixes: QTBUG-86017
Change-Id: I49b7498a3d03141b654e453a3c35a43fc7ba804a
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
-rw-r--r-- | src/qmlmodels/qqmldelegatemodel.cpp | 2 | ||||
-rw-r--r-- | src/quick/items/qquickitemview.cpp | 4 | ||||
-rw-r--r-- | tests/auto/qml/qqmldelegatemodel/data/qtbug_86017.qml | 32 | ||||
-rw-r--r-- | tests/auto/qml/qqmldelegatemodel/tst_qqmldelegatemodel.cpp | 16 |
4 files changed, 53 insertions, 1 deletions
diff --git a/src/qmlmodels/qqmldelegatemodel.cpp b/src/qmlmodels/qqmldelegatemodel.cpp index 303ca0e2e4..838a0e18e2 100644 --- a/src/qmlmodels/qqmldelegatemodel.cpp +++ b/src/qmlmodels/qqmldelegatemodel.cpp @@ -1340,6 +1340,8 @@ QObject *QQmlDelegateModel::object(int index, QQmlIncubator::IncubationMode incu QQmlIncubator::Status QQmlDelegateModel::incubationStatus(int index) { Q_D(QQmlDelegateModel); + if (d->m_compositor.count(d->m_compositorGroup) <= index) + return QQmlIncubator::Null; Compositor::iterator it = d->m_compositor.find(d->m_compositorGroup, index); if (!it->inCache()) return QQmlIncubator::Null; diff --git a/src/quick/items/qquickitemview.cpp b/src/quick/items/qquickitemview.cpp index 95b5415ef0..f1336a91f6 100644 --- a/src/quick/items/qquickitemview.cpp +++ b/src/quick/items/qquickitemview.cpp @@ -2346,7 +2346,9 @@ FxViewItem *QQuickItemViewPrivate::createItem(int modelIndex, QQmlIncubator::Inc inRequest = true; - QObject* object = model->object(modelIndex, incubationMode); + // The model will run this same range check internally but produce a warning and return nullptr. + // Since we handle this result graciously in our code, we preempt this warning by checking the range ourselves. + QObject* object = modelIndex < model->count() ? model->object(modelIndex, incubationMode) : nullptr; QQuickItem *item = qmlobject_cast<QQuickItem*>(object); if (!item) { diff --git a/tests/auto/qml/qqmldelegatemodel/data/qtbug_86017.qml b/tests/auto/qml/qqmldelegatemodel/data/qtbug_86017.qml new file mode 100644 index 0000000000..02d737e37f --- /dev/null +++ b/tests/auto/qml/qqmldelegatemodel/data/qtbug_86017.qml @@ -0,0 +1,32 @@ +import QtQuick 2.8 +import QtQml.Models 2.1 + +DelegateModel { + id: visualModel + model: ListModel { + id: myLM + ListElement { + name: "Apple" + } + ListElement { + name: "Orange" + } + } + + filterOnGroup: "selected" + + groups: [ + DelegateModelGroup { + name: "selected" + includeByDefault: true + } + ] + + delegate: Text { + Component.onCompleted: { + DelegateModel.inPersistedItems = true + DelegateModel.inSelected = false + } + text: "item " + index + } +} diff --git a/tests/auto/qml/qqmldelegatemodel/tst_qqmldelegatemodel.cpp b/tests/auto/qml/qqmldelegatemodel/tst_qqmldelegatemodel.cpp index 87f42c0c8a..8e6d147028 100644 --- a/tests/auto/qml/qqmldelegatemodel/tst_qqmldelegatemodel.cpp +++ b/tests/auto/qml/qqmldelegatemodel/tst_qqmldelegatemodel.cpp @@ -42,6 +42,7 @@ public: private slots: void valueWithoutCallingObjectFirst_data(); void valueWithoutCallingObjectFirst(); + void qtbug_86017(); }; class AbstractItemModel : public QAbstractItemModel @@ -134,6 +135,21 @@ void tst_QQmlDelegateModel::valueWithoutCallingObjectFirst() QCOMPARE(model->variantValue(index, role), expectedValue); } +void tst_QQmlDelegateModel::qtbug_86017() +{ + QQmlEngine engine; + QQmlComponent component(&engine); + component.loadUrl(testFileUrl("qtbug_86017.qml")); + QScopedPointer<QObject> root(component.create()); + QVERIFY2(root, qPrintable(component.errorString())); + QTRY_VERIFY(component.isReady()); + QQmlDelegateModel *model = qobject_cast<QQmlDelegateModel*>(root.data()); + + QVERIFY(model); + QCOMPARE(model->count(), 2); + QCOMPARE(model->filterGroup(), "selected"); +} + QTEST_MAIN(tst_QQmlDelegateModel) #include "tst_qqmldelegatemodel.moc" |