QtQuick: Simplify TableView model handling

Unwrap any QJSValues right away and eliminate a copy of the model.

Pick-to: 6.10
Task-number: QTBUG-139941
Change-Id: I7f4a3ea97ae64cf0bb24aa032f8307c56bf7a597
Reviewed-by: Santhosh Kumar <santhosh.kumar.selvaraj@qt.io>
This commit is contained in:
Ulf Hermann 2025-09-11 13:34:02 +02:00
parent ae8ec32a99
commit 9494ecafb9
3 changed files with 17 additions and 24 deletions

View File

@ -4585,20 +4585,20 @@ void QQuickTableViewPrivate::setModelImpl(const QVariant &newModel)
void QQuickTableViewPrivate::syncModel()
{
if (compareModel(modelVariant, assignedModel))
if (tableModel) {
if (tableModel->model() == assignedModel)
return;
} else if (QVariant::fromValue(model) == assignedModel) {
return;
}
if (model) {
disconnectFromModel();
releaseLoadedItems(QQmlTableInstanceModel::NotReusable);
}
modelVariant = assignedModel;
QVariant effectiveModelVariant = modelVariant;
if (effectiveModelVariant.userType() == qMetaTypeId<QJSValue>())
effectiveModelVariant = effectiveModelVariant.value<QJSValue>().toVariant();
const auto instanceModel = qobject_cast<QQmlInstanceModel *>(qvariant_cast<QObject*>(effectiveModelVariant));
const auto instanceModel = qobject_cast<QQmlInstanceModel *>(
qvariant_cast<QObject *>(assignedModel));
if (instanceModel) {
if (tableModel) {
@ -4609,7 +4609,7 @@ void QQuickTableViewPrivate::syncModel()
} else {
if (!tableModel)
createWrapperModel();
tableModel->setModel(effectiveModelVariant);
tableModel->setModel(assignedModel);
}
connectToModel();
@ -4875,13 +4875,6 @@ void QQuickTableViewPrivate::modelResetCallback()
scheduleRebuildTable(RebuildOption::All);
}
bool QQuickTableViewPrivate::compareModel(const QVariant& model1, const QVariant& model2) const
{
return (model1 == model2 ||
(model1.userType() == qMetaTypeId<QJSValue>() && model2.userType() == qMetaTypeId<QJSValue>() &&
model1.value<QJSValue>().strictlyEquals(model2.value<QJSValue>())));
}
void QQuickTableViewPrivate::positionViewAtRow(int row, Qt::Alignment alignment, qreal offset, const QRectF subRect)
{
Qt::Alignment verticalAlignment = alignment & (Qt::AlignTop | Qt::AlignVCenter | Qt::AlignBottom);
@ -5798,11 +5791,16 @@ QVariant QQuickTableView::model() const
void QQuickTableView::setModel(const QVariant &newModel)
{
Q_D(QQuickTableView);
if (d->compareModel(newModel, d->assignedModel))
QVariant model = newModel;
if (model.userType() == qMetaTypeId<QJSValue>())
model = model.value<QJSValue>().toVariant();
if (model == d->assignedModel)
return;
closeEditor();
d->setModelImpl(newModel);
d->setModelImpl(model);
if (d->selectionModel)
d->selectionModel->setModel(d->selectionSourceModel());
}

View File

@ -334,7 +334,6 @@ public:
// we need a pointer for that case as well.
QQmlInstanceModel* model = nullptr;
QPointer<QQmlTableInstanceModel> tableModel = nullptr;
QVariant modelVariant;
// When the applications assignes a new model or delegate to the view, we keep them
// around until we're ready to take them into use (syncWithPendingChanges).

View File

@ -314,13 +314,9 @@ void QQuickTreeViewPrivate::setModelImpl(const QVariant &newModel)
Q_Q(QQuickTreeView);
m_assignedModel = newModel;
QVariant effectiveModel = m_assignedModel;
if (effectiveModel.userType() == qMetaTypeId<QJSValue>())
effectiveModel = effectiveModel.value<QJSValue>().toVariant();
if (effectiveModel.isNull())
if (m_assignedModel.isNull())
m_treeModelToTableModel.setModel(nullptr);
else if (const auto qaim = qvariant_cast<QAbstractItemModel*>(effectiveModel))
else if (const auto qaim = qvariant_cast<QAbstractItemModel *>(m_assignedModel))
m_treeModelToTableModel.setModel(qaim);
else
qmlWarning(q) << "TreeView only accepts a model of type QAbstractItemModel";