diff --git a/src/corelib/itemmodels/qitemselectionmodel.cpp b/src/corelib/itemmodels/qitemselectionmodel.cpp index 6c7101d41fa..7714aa5e466 100644 --- a/src/corelib/itemmodels/qitemselectionmodel.cpp +++ b/src/corelib/itemmodels/qitemselectionmodel.cpp @@ -942,13 +942,14 @@ static QItemSelection mergeRowLengths(const QVector; + using IntPairList = QList; + using IntPairPair = std::pair; + using IntPairPairList = QList; + + QTest::addColumn("rangesToSelect"); + QTest::addColumn("expectedSelectedIndexesPairs"); + QTest::newRow("Single index in > 0 column") + << (IntPairPairList() << IntPairPair(IntPair(0, 1), IntPair(0, 1))) + << (IntPairList() << IntPair(0, 1)); + QTest::newRow("Rectangle in > 0 column") + << (IntPairPairList() << IntPairPair(IntPair(0, 1), IntPair(1, 2))) + << (IntPairList() << IntPair(0, 1) << IntPair(0, 2) << IntPair(1, 1) << IntPair(1, 2)); + QTest::newRow("Diagonal in > 0 column") + << (IntPairPairList() + << IntPairPair(IntPair(0, 1), IntPair(0, 1)) + << IntPairPair(IntPair(1, 2), IntPair(1, 2)) + << IntPairPair(IntPair(2, 3), IntPair(2, 3))) + << (IntPairList() + << IntPair(0, 1) + << IntPair(1, 2) + << IntPair(2, 3)); +} + +void tst_QItemSelectionModel::QTBUG58851() +{ + using IntPair = std::pair; + using IntPairList = QList; + using IntPairPair = std::pair; + using IntPairPairList = QList; + + QFETCH(IntPairPairList, rangesToSelect); + QFETCH(IntPairList, expectedSelectedIndexesPairs); + + QStandardItemModel model(4, 4); + for (int row = 0; row < model.rowCount(); ++row) { + for (int column = 0; column < model.columnCount(); ++column) { + QStandardItem *item = new QStandardItem(QString("%0%1").arg(row).arg(column)); + model.setItem(row, column, item); + } + } + + QSortFilterProxyModel proxy; + proxy.setSourceModel(&model); + proxy.setSortRole(Qt::DisplayRole); + + std::vector expectedSelectedIndexes; + for (const IntPair &index : expectedSelectedIndexesPairs) + expectedSelectedIndexes.emplace_back(proxy.index(index.first, index.second)); + + QItemSelectionModel selections(&proxy); + for (const IntPairPair &range : rangesToSelect) { + const IntPair &tl = range.first; + const IntPair &br = range.second; + selections.select(QItemSelection(proxy.index(tl.first, tl.second), + proxy.index(br.first, br.second)), + QItemSelectionModel::Select); + } + + for (const QPersistentModelIndex &i : expectedSelectedIndexes) { + QVERIFY(selections.isSelected(i)); + } + proxy.sort(1, Qt::DescendingOrder); + QCOMPARE(selections.selectedIndexes().count(), (int)expectedSelectedIndexes.size()); + for (const QPersistentModelIndex &i : expectedSelectedIndexes) { + QVERIFY(selections.isSelected(i)); + } +} + QTEST_MAIN(tst_QItemSelectionModel) #include "tst_qitemselectionmodel.moc"