aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick/items/qquicktableview.cpp
diff options
context:
space:
mode:
authorRichard Moe Gustavsen <richard.gustavsen@qt.io>2022-04-07 15:03:19 +0200
committerRichard Moe Gustavsen <richard.gustavsen@qt.io>2022-04-11 19:37:20 +0200
commitb9dcf12a463026d09b6bfa4ed213f62a480fb5f2 (patch)
treeb6fe14143b2ff460d93a9c3c63619acd4821e3f4 /src/quick/items/qquicktableview.cpp
parent19d19864166e03405cb145de8141f905d0a6d4fd (diff)
QQuickTableView: keep selection model and tableview model in sync
The source model in the selection model will always need to be the same as the source model in TableView. So TableView might as well forward its own model to the selection model, so that the user don't need to worry about setting the model explicitly. The same is also done in QTableView. Still, it's always possible to force (perhaps by accident?) the two models to be different. To avoid confusion, we choose to print a warning if that happens. Change-Id: I9fec1d3de4cfe83cf28950ebdedd38a010df16a3 Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/quick/items/qquicktableview.cpp')
-rw-r--r--src/quick/items/qquicktableview.cpp25
1 files changed, 20 insertions, 5 deletions
diff --git a/src/quick/items/qquicktableview.cpp b/src/quick/items/qquicktableview.cpp
index 2ef65145d0..e7919056be 100644
--- a/src/quick/items/qquicktableview.cpp
+++ b/src/quick/items/qquicktableview.cpp
@@ -3235,6 +3235,14 @@ void QQuickTableViewPrivate::setSelectedOnDelegateItem(const QModelIndex &modelI
setRequiredProperty(kRequiredProperty_selected, QVariant::fromValue(select), cellIndex, item, false);
}
+QAbstractItemModel *QQuickTableViewPrivate::qaim(QVariant modelAsVariant) const
+{
+ // If modelAsVariant wraps a qaim, return it
+ if (modelAsVariant.userType() == qMetaTypeId<QJSValue>())
+ modelAsVariant = modelAsVariant.value<QJSValue>().toVariant();
+ return qvariant_cast<QAbstractItemModel *>(modelAsVariant);
+}
+
void QQuickTableViewPrivate::updateSelectedOnAllDelegateItems()
{
for (auto it = loadedItems.keyBegin(), end = loadedItems.keyEnd(); it != end; ++it) {
@@ -3250,6 +3258,12 @@ void QQuickTableViewPrivate::updateSelectedOnAllDelegateItems()
void QQuickTableViewPrivate::currentChangedInSelectionModel(const QModelIndex &current, const QModelIndex &previous)
{
+ // Warn if the source models are not the same
+ const QAbstractItemModel *qaimInSelection = selectionModel ? selectionModel->model() : nullptr;
+ const QAbstractItemModel *qaimInTableView = qaim(modelImpl());
+ if (qaimInSelection && qaimInSelection != qaimInTableView)
+ qmlWarning(q_func()) << "TableView.selectionModel.model differs from TableView.model";
+
setCurrentOnDelegateItem(previous, false);
setCurrentOnDelegateItem(current, true);
}
@@ -3968,10 +3982,6 @@ void QQuickTableViewPrivate::setCurrentIndex(const QPoint &cell)
if (!selectionModel)
return;
- const QAbstractItemModel *qaim = selectionModel->model();
- if (!qaim)
- return;
-
const auto index = q_func()->modelIndex(cell);
selectionModel->setCurrentIndex(index, QItemSelectionModel::NoUpdate);
}
@@ -4120,7 +4130,11 @@ QVariant QQuickTableView::model() const
void QQuickTableView::setModel(const QVariant &newModel)
{
- return d_func()->setModelImpl(newModel);
+ Q_D(QQuickTableView);
+ d->setModelImpl(newModel);
+
+ if (d->selectionModel)
+ d->selectionModel->setModel(d->qaim(newModel));
}
QQmlComponent *QQuickTableView::delegate() const
@@ -4271,6 +4285,7 @@ void QQuickTableView::setSelectionModel(QItemSelectionModel *selectionModel)
d->selectionModel = selectionModel;
if (d->selectionModel) {
+ d->selectionModel->setModel(d->qaim(d->modelImpl()));
QQuickTableViewPrivate::connect(d->selectionModel, &QItemSelectionModel::selectionChanged,
d, &QQuickTableViewPrivate::selectionChangedInSelectionModel);
QQuickTableViewPrivate::connect(d->selectionModel, &QItemSelectionModel::currentChanged,