diff options
author | Ulf Hermann <[email protected]> | 2025-09-11 13:34:02 +0200 |
---|---|---|
committer | Ulf Hermann <[email protected]> | 2025-09-13 10:15:56 +0200 |
commit | 9494ecafb9b378d2336bdf2257a5089919532416 (patch) | |
tree | 99fe16965a6df744fba8c2262bcc3675380857ef | |
parent | ae8ec32a99191aca3c549730bf26ef126dd38c82 (diff) |
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 <[email protected]>
-rw-r--r-- | src/quick/items/qquicktableview.cpp | 32 | ||||
-rw-r--r-- | src/quick/items/qquicktableview_p_p.h | 1 | ||||
-rw-r--r-- | src/quick/items/qquicktreeview.cpp | 8 |
3 files changed, 17 insertions, 24 deletions
diff --git a/src/quick/items/qquicktableview.cpp b/src/quick/items/qquicktableview.cpp index fbb1b6ed08..6c8ace80e6 100644 --- a/src/quick/items/qquicktableview.cpp +++ b/src/quick/items/qquicktableview.cpp @@ -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()); } diff --git a/src/quick/items/qquicktableview_p_p.h b/src/quick/items/qquicktableview_p_p.h index 70ddf05f98..3db877e4a3 100644 --- a/src/quick/items/qquicktableview_p_p.h +++ b/src/quick/items/qquicktableview_p_p.h @@ -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). diff --git a/src/quick/items/qquicktreeview.cpp b/src/quick/items/qquicktreeview.cpp index a63a8f278d..33be219db0 100644 --- a/src/quick/items/qquicktreeview.cpp +++ b/src/quick/items/qquicktreeview.cpp @@ -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"; |