From 330e73ccf1c7b541c81eae9347260a7ef383843a Mon Sep 17 00:00:00 2001 From: Michael Brasser Date: Wed, 29 Mar 2017 13:47:40 -0500 Subject: Ensure selection model updates when changed at source Change-Id: I37d037428107c7ab1666d6b181fb0c9e465c6348 Reviewed-by: Brett Stottlemyer --- .../qremoteobjectabstractitemmodeltypes.h | 2 + tests/auto/modelview/tst_modelview.cpp | 43 ++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/src/remoteobjects/qremoteobjectabstractitemmodeltypes.h b/src/remoteobjects/qremoteobjectabstractitemmodeltypes.h index 42773a5..385fdd0 100644 --- a/src/remoteobjects/qremoteobjectabstractitemmodeltypes.h +++ b/src/remoteobjects/qremoteobjectabstractitemmodeltypes.h @@ -179,6 +179,8 @@ inline QString modelIndexToString(const ModelIndex &index) inline QModelIndex toQModelIndex(const IndexList &list, const QAbstractItemModel *model, bool *ok = nullptr, bool ensureItem = false) { + if (ok) + *ok = true; QModelIndex result; for (int i = 0; i < list.count(); ++i) { const ModelIndex &index = list[i]; diff --git a/tests/auto/modelview/tst_modelview.cpp b/tests/auto/modelview/tst_modelview.cpp index bdc1de0..dbcfcfd 100644 --- a/tests/auto/modelview/tst_modelview.cpp +++ b/tests/auto/modelview/tst_modelview.cpp @@ -622,6 +622,9 @@ private slots: void testModelTest(); void testSortFilterModel(); + void testSelectionFromReplica(); + void testSelectionFromSource(); + void cleanup(); }; @@ -1173,6 +1176,46 @@ void TestModelView::testSetDataTree() compareData(&m_sourceModel, model.data()); } +void TestModelView::testSelectionFromReplica() +{ + QVector roles = QVector() << Qt::DisplayRole << Qt::BackgroundRole; + QStandardItemModel simpleModel; + for (int i = 0; i < 4; ++i) + simpleModel.appendRow(new QStandardItem(QString("item %0").arg(i))); + QItemSelectionModel selectionModel(&simpleModel); + m_basicServer.enableRemoting(&simpleModel, "simpleModel", roles, &selectionModel); + + QScopedPointer model(m_client.acquireModel("simpleModel")); + QItemSelectionModel *replicaSelectionModel = model->selectionModel(); + + FetchData f(model.data()); + f.addAll(); + QVERIFY(f.fetchAndWait()); + + replicaSelectionModel->setCurrentIndex(model->index(1,0), QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Current); + QTRY_COMPARE(selectionModel.currentIndex().row(), 1); +} + +void TestModelView::testSelectionFromSource() +{ + QVector roles = QVector() << Qt::DisplayRole << Qt::BackgroundRole; + QStandardItemModel simpleModel; + for (int i = 0; i < 4; ++i) + simpleModel.appendRow(new QStandardItem(QString("item %0").arg(i))); + QItemSelectionModel selectionModel(&simpleModel); + m_basicServer.enableRemoting(&simpleModel, "simpleModel", roles, &selectionModel); + + QScopedPointer model(m_client.acquireModel("simpleModel")); + QItemSelectionModel *replicaSelectionModel = model->selectionModel(); + + FetchData f(model.data()); + f.addAll(); + QVERIFY(f.fetchAndWait()); + + selectionModel.setCurrentIndex(simpleModel.index(1,0), QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Current); + QTRY_COMPARE(replicaSelectionModel->currentIndex().row(), 1); +} + void TestModelView::cleanup() { // wait for delivery of RemoveObject events to the source -- cgit v1.2.3