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>
This commit is contained in:
Richard Moe Gustavsen 2019-12-05 15:29:29 +01:00
parent 6bc4d55fe1
commit f60f2aaa51
3 changed files with 22 additions and 20 deletions

View File

@ -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;
}

View File

@ -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

View File

@ -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)