Skip to content
/ qtbase Public

Commit 3b21583

Browse files
vohiQt Cherry-pick Bot
authored andcommitted
QRMA: unconstify root index member for tree adapters
While only set during construction, making it const breaks assignment. Add a test case that implement a search algorithm that finds a matching row within a tree hierarchy. For that to work (withing using an expensive recursive implementation) we need an assignment operator. Change-Id: I52c3504acbd9a5196777c952e69b177de9abcddd Reviewed-by: Artem Dyomin <artem.dyomin@qt.io> (cherry picked from commit 4d7dfcf) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
1 parent 73a97ee commit 3b21583

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/corelib/itemmodels/qrangemodeladapter_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ struct ParentIndex
361361
template <>
362362
struct ParentIndex<true>
363363
{
364-
const QModelIndex m_rootIndex;
364+
QModelIndex m_rootIndex;
365365
QModelIndex root() const { return m_rootIndex; }
366366
};
367367

tests/auto/corelib/itemmodels/qrangemodeladapter/tst_qrangemodeladapter.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ private slots:
7777
void treeIterate();
7878
void treeAccess();
7979
void treeWriteAccess();
80+
void treeFindRecursively();
8081

8182
void insertRow();
8283
void insertRows();
@@ -2038,6 +2039,29 @@ void tst_QRangeModelAdapter::treeWriteAccess()
20382039
}
20392040
}
20402041

2042+
void tst_QRangeModelAdapter::treeFindRecursively()
2043+
{
2044+
QRangeModelAdapter adapter(createValueTree());
2045+
QStringView needle = u"2.3.2";
2046+
QList<int> path;
2047+
while (true) {
2048+
auto it = std::find_if(adapter.cbegin(), adapter.cend(), [needle](const auto &row){
2049+
return needle.startsWith(row->value());
2050+
});
2051+
QVERIFY(it != adapter.cend());
2052+
path.append(it - adapter.cbegin());
2053+
if (it->value() == needle)
2054+
break;
2055+
if ((*it).hasChildren()) {
2056+
adapter = (*it).children();
2057+
} else {
2058+
path = {};
2059+
break;
2060+
}
2061+
}
2062+
QCOMPARE(path, (QList<int>{1, 2, 1}));
2063+
}
2064+
20412065
void tst_QRangeModelAdapter::insertRow()
20422066
{
20432067
{

0 commit comments

Comments
 (0)