diff options
author | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2019-12-05 15:29:29 +0100 |
---|---|---|
committer | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2019-12-09 14:16:07 +0100 |
commit | f60f2aaa5176ca703fa01fbaf01059fe989c0029 (patch) | |
tree | 54ba9a24ac9af8a436545aa6f0f66be79eb2370b | |
parent | 6bc4d55fe196a6310cadb239c6069ad2ea044d05 (diff) |
QQuickTableView: ensure we release items in the old model and not the new
As it stood, we would wait to release loaded items until we started the
rebuild process, if the old model was a DelegateModel. But at that time,
the model would alread have been changed, so we would release the items
by calling out to the wrong model.
This patch will ensure that we always release the items immediately when
syncing the model, which will also cover the case when the model is a
DelegateModel.
Fixes: QTBUG-80570
Change-Id: I1b06011f4795727d04d9cd8c20381f65552b8fe8
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
-rw-r--r-- | src/quick/items/qquicktableview.cpp | 15 | ||||
-rw-r--r-- | tests/auto/quick/qquicktableview/data/replaceModelTableView.qml | 13 | ||||
-rw-r--r-- | tests/auto/quick/qquicktableview/tst_qquicktableview.cpp | 14 |
3 files changed, 22 insertions, 20 deletions
diff --git a/src/quick/items/qquicktableview.cpp b/src/quick/items/qquicktableview.cpp index 73857af558..ab57a8ea0b 100644 --- a/src/quick/items/qquicktableview.cpp +++ b/src/quick/items/qquicktableview.cpp @@ -1786,10 +1786,12 @@ void QQuickTableViewPrivate::beginRebuildTable() QPointF topLeftPos; calculateTopLeft(topLeft, topLeftPos); - if (rebuildOptions & RebuildOption::All) - releaseLoadedItems(QQmlTableInstanceModel::NotReusable); - else if (rebuildOptions & RebuildOption::ViewportOnly) - releaseLoadedItems(reusableFlag); + if (!loadedItems.isEmpty()) { + if (rebuildOptions & RebuildOption::All) + releaseLoadedItems(QQmlTableInstanceModel::NotReusable); + else if (rebuildOptions & RebuildOption::ViewportOnly) + releaseLoadedItems(reusableFlag); + } if (rebuildOptions & RebuildOption::All) { origin = QPointF(0, 0); @@ -2237,8 +2239,10 @@ void QQuickTableViewPrivate::syncModel() if (modelVariant == assignedModel) return; - if (model) + if (model) { disconnectFromModel(); + releaseLoadedItems(QQmlTableInstanceModel::NotReusable); + } modelVariant = assignedModel; QVariant effectiveModelVariant = modelVariant; @@ -2249,7 +2253,6 @@ void QQuickTableViewPrivate::syncModel() if (instanceModel) { if (tableModel) { - releaseLoadedItems(QQmlTableInstanceModel::NotReusable); delete tableModel; tableModel = nullptr; } diff --git a/tests/auto/quick/qquicktableview/data/replaceModelTableView.qml b/tests/auto/quick/qquicktableview/data/replaceModelTableView.qml index 1b6074f5d5..cc109bb469 100644 --- a/tests/auto/quick/qquicktableview/data/replaceModelTableView.qml +++ b/tests/auto/quick/qquicktableview/data/replaceModelTableView.qml @@ -8,6 +8,9 @@ Item { height: 480 property alias tableView: tv + property alias objectModel: om + property alias listModel: lm + property alias delegateModel: dm ObjectModel { id: om @@ -38,16 +41,6 @@ Item { id: tv visible: true anchors.fill: parent - property int modelId: 0 - - model: { - switch (modelId) { - case 0: return lm; - case 1: return om; - case 2: return dm; - default: return null; - } - } delegate: Rectangle { id: dlg diff --git a/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp b/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp index 09d1b8ca7c..405524b704 100644 --- a/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp +++ b/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp @@ -2705,14 +2705,20 @@ void tst_QQuickTableView::replaceModel() { LOAD_TABLEVIEW("replaceModelTableView.qml"); - tableView->setProperty("modelId", 0); + const auto objectModel = view->rootObject()->property("objectModel"); + const auto listModel = view->rootObject()->property("listModel"); + const auto delegateModel = view->rootObject()->property("delegateModel"); + + tableView->setModel(listModel); QTRY_COMPARE(tableView->rows(), 2); - tableView->setProperty("modelId", 1); + tableView->setModel(objectModel); QTRY_COMPARE(tableView->rows(), 3); - tableView->setProperty("modelId", 2); + tableView->setModel(delegateModel); QTRY_COMPARE(tableView->rows(), 2); - tableView->setProperty("modelId", 0); + tableView->setModel(listModel); QTRY_COMPARE(tableView->rows(), 2); + tableView->setModel(QVariant()); + QTRY_COMPARE(tableView->rows(), 0); } QTEST_MAIN(tst_QQuickTableView) |