diff options
author | Andreas Buhr <andreas.buhr@qt.io> | 2021-04-30 09:07:06 +0200 |
---|---|---|
committer | Andreas Buhr <andreas.buhr@qt.io> | 2021-05-18 08:49:56 +0200 |
commit | 8278879c199ecc9b17a1fd603ecb4a31f5fd6c68 (patch) | |
tree | 164bd1f169ccb404fe174d656814934773d613f4 /tests/auto/corelib/itemmodels | |
parent | 0ed6fd77a0658414f2c8d104939e7de68477a80c (diff) |
Fix QItemSelectionModel::selectionChanged emission
QItemSelectionModel has the property selectedIndexes with
the notification signal selectionChanged. When a row is deleted
or inserted above the current selection, the row number of the
current selection changes and thus the return value of
selectedIndexes changes. This should trigger its notification
signal.
This signal was not emitted. This patch fixes this and
adds a unit test to verify this.
[ChangeLog][Important Behavior Changes][QtCore]
QItemSelectionModel now emits the selectionChanged signal
if only the indexes of the selected items change.
Fixes: QTBUG-93305
Change-Id: Ia5fb5ca32d658c9c0e1d7093c57cc08a966b9402
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
Reviewed-by: David Faure <david.faure@kdab.com>
Diffstat (limited to 'tests/auto/corelib/itemmodels')
-rw-r--r-- | tests/auto/corelib/itemmodels/qitemselectionmodel/tst_qitemselectionmodel.cpp | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/tests/auto/corelib/itemmodels/qitemselectionmodel/tst_qitemselectionmodel.cpp b/tests/auto/corelib/itemmodels/qitemselectionmodel/tst_qitemselectionmodel.cpp index ceca96f791..b7e13c8853 100644 --- a/tests/auto/corelib/itemmodels/qitemselectionmodel/tst_qitemselectionmodel.cpp +++ b/tests/auto/corelib/itemmodels/qitemselectionmodel/tst_qitemselectionmodel.cpp @@ -103,6 +103,8 @@ private slots: void QTBUG18001_data(); void QTBUG18001(); + void QTBUG93305(); + private: QAbstractItemModel *model; QItemSelectionModel *selection; @@ -2897,5 +2899,46 @@ void tst_QItemSelectionModel::QTBUG18001() } +void tst_QItemSelectionModel::QTBUG93305() +{ + // make sure the model is sane (5x5) + QCOMPARE(model->rowCount(QModelIndex()), 5); + QCOMPARE(model->columnCount(QModelIndex()), 5); + + QSignalSpy spy(selection, &QItemSelectionModel::selectionChanged); + + // select in row 1 + QModelIndex index = model->index(1, 0, QModelIndex()); + selection->select(index, QItemSelectionModel::ClearAndSelect); + QVERIFY(selection->hasSelection()); + QCOMPARE(spy.count(), 1); + + // removing row 0 does not change which cells are selected, but it + // does change the row number of the selected cells. Thus it changes + // what selectedIndexes() returns. + // The property selectedIndexes() has selectionChanged() as its + // NOTIFY signal, so selectionChanged() should be emitted. + + // delete row 0 + model->removeRows(0, 1, QModelIndex()); + QVERIFY(selection->hasSelection()); + QCOMPARE(spy.count(), 2); + + // inserting a row before the first row again does not change which cells + // are selected, but does change the row number of the selected cells. + // This changes what selectedIndexes() returns and should thus trigger + // a selectionChanged() signal + + // insert row 0 again + model->insertRows(0, 1, QModelIndex()); + QVERIFY(selection->hasSelection()); + QCOMPARE(spy.count(), 3); + + // test for inserting multiple (6) rows + model->insertRows(0, 6, QModelIndex()); + QVERIFY(selection->hasSelection()); + QCOMPARE(spy.count(), 4); +} + QTEST_MAIN(tst_QItemSelectionModel) #include "tst_qitemselectionmodel.moc" |