QQuickTableView: support assigning a DelegateModel

Normally you either assign a model to TableView that
already has a delegate (or don't need one), like
DelegateModel or ObjectModel. Or instead you assign
a QAIM model and a delegate directly. But if you
assign both a delegate and an ObjectModel, TableView
would be confused, and ignore the assigned model
and instead create an internal wrapper model that
ends up empty.

This patch will ensure that we don't create a wrapper
model in such cases, but instead forward the
delegate to whichever model is assigned, even
if it ends up as a no-op for models that don't
use one.

Task-number: QTBUG-80534
Change-Id: Idd220df08617c379dc7808ee1f41c862b78cc201
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
This commit is contained in:
Richard Moe Gustavsen 2019-12-04 13:07:06 +01:00
parent 3770696ce5
commit 6bc4d55fe1
3 changed files with 18 additions and 14 deletions

View File

@ -1054,12 +1054,14 @@ void QQuickTableViewPrivate::releaseLoadedItems(QQmlTableInstanceModel::Reusable
void QQuickTableViewPrivate::releaseItem(FxTableItem *fxTableItem, QQmlTableInstanceModel::ReusableFlag reusableFlag)
{
Q_Q(QQuickTableView);
// Note that fxTableItem->item might already have been destroyed, in case
// the item is owned by the QML context rather than the model (e.g ObjectModel etc).
auto item = fxTableItem->item;
Q_TABLEVIEW_ASSERT(item, fxTableItem->index);
if (fxTableItem->ownItem) {
Q_TABLEVIEW_ASSERT(item, fxTableItem->index);
delete item;
} else {
} else if (item) {
// Only QQmlTableInstanceModel supports reusing items
auto releaseFlag = tableModel ?
tableModel->release(item, reusableFlag) :
@ -2219,12 +2221,14 @@ void QQuickTableViewPrivate::syncRebuildOptions()
void QQuickTableViewPrivate::syncDelegate()
{
if (tableModel && assignedDelegate == tableModel->delegate())
if (!tableModel) {
// Only the tableModel uses the delegate assigned to a
// TableView. DelegateModel has it's own delegate, and
// ObjectModel etc. doesn't use one.
return;
}
if (!tableModel)
createWrapperModel();
if (assignedDelegate != tableModel->delegate())
tableModel->setDelegate(assignedDelegate);
}

View File

@ -11,9 +11,9 @@ Item {
ObjectModel {
id: om
Rectangle { height: 30; width: 80; color: "red" }
Rectangle { height: 30; width: 80; color: "green" }
Rectangle { height: 30; width: 80; color: "blue" }
Rectangle { implicitHeight: 30; implicitWidth: 80; color: "red" }
Rectangle { implicitHeight: 30; implicitWidth: 80; color: "green" }
Rectangle { implicitHeight: 30; implicitWidth: 80; color: "blue" }
}
ListModel {
@ -29,8 +29,8 @@ Item {
ListElement { name: "Orange" }
}
delegate: Rectangle {
height: 25
width: 100
implicitHeight: 25
implicitWidth: 100
Text { text: "Name: " + name}
}
}

View File

@ -2708,9 +2708,9 @@ void tst_QQuickTableView::replaceModel()
tableView->setProperty("modelId", 0);
QTRY_COMPARE(tableView->rows(), 2);
tableView->setProperty("modelId", 1);
QTRY_COMPARE(tableView->rows(), 0);
QTRY_COMPARE(tableView->rows(), 3);
tableView->setProperty("modelId", 2);
QTRY_COMPARE(tableView->rows(), 0);
QTRY_COMPARE(tableView->rows(), 2);
tableView->setProperty("modelId", 0);
QTRY_COMPARE(tableView->rows(), 2);
}