aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/quick/qquicktreeview/tst_qquicktreeview.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/quick/qquicktreeview/tst_qquicktreeview.cpp')
-rw-r--r--tests/auto/quick/qquicktreeview/tst_qquicktreeview.cpp223
1 files changed, 184 insertions, 39 deletions
diff --git a/tests/auto/quick/qquicktreeview/tst_qquicktreeview.cpp b/tests/auto/quick/qquicktreeview/tst_qquicktreeview.cpp
index 6e7a1e1216..d41d811496 100644
--- a/tests/auto/quick/qquicktreeview/tst_qquicktreeview.cpp
+++ b/tests/auto/quick/qquicktreeview/tst_qquicktreeview.cpp
@@ -1,5 +1,5 @@
// Copyright (C) 2021 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QtTest/QtTest>
#include <QtQuickTest/quicktest.h>
@@ -76,6 +76,7 @@ private slots:
void emptyModel();
void updatedModifiedModel();
void insertRows();
+ void insertColumns();
void toggleExpandedUsingArrowKeys();
void expandAndCollapsUsingDoubleClick();
void selectionBehaviorCells_data();
@@ -87,6 +88,8 @@ private slots:
void sortTreeModel();
void sortTreeModelDynamic_data();
void sortTreeModelDynamic();
+ void setRootIndex();
+ void setRootIndexToLeaf();
};
tst_qquicktreeview::tst_qquicktreeview()
@@ -139,7 +142,7 @@ void tst_qquicktreeview::expandAndCollapseRoot()
// Check that the view only has one row loaded so far (the root of the tree)
QCOMPARE(treeViewPrivate->loadedRows.count(), 1);
- QSignalSpy expandedSpy(treeView, SIGNAL(expanded(int, int)));
+ QSignalSpy expandedSpy(treeView, SIGNAL(expanded(int,int)));
// Expand the root
treeView->expand(0);
@@ -184,7 +187,7 @@ void tst_qquicktreeview::expandAndCollapseChildren()
LOAD_TREEVIEW("normaltreeview.qml");
const int childCount = 4;
- QSignalSpy expandedSpy(treeView, SIGNAL(expanded(int, int)));
+ QSignalSpy expandedSpy(treeView, SIGNAL(expanded(int,int)));
// Expand the last child of a parent recursively four times
for (int level = 0; level < 4; ++level) {
@@ -305,7 +308,7 @@ void tst_qquicktreeview::requiredPropertiesChildren()
QCOMPARE(viewProp, treeView);
QCOMPARE(isTreeNode, true);
QCOMPARE(expanded, row == 4);
- QCOMPARE(hasChildren, model->hasChildren(treeView->modelIndex(row, 0)));
+ QCOMPARE(hasChildren, model->hasChildren(treeView->index(row, 0)));
QCOMPARE(depth, row <= 4 ? 1 : 2);
}
}
@@ -328,7 +331,7 @@ void tst_qquicktreeview::emptyModel()
QCOMPARE(treeView->depth(0), -1);
QCOMPARE(treeView->isExpanded(0), false);
- QVERIFY(!treeView->modelIndex(10, 10).isValid());
+ QVERIFY(!treeView->index(10, 10).isValid());
QCOMPARE(treeView->rowAtIndex(QModelIndex()), -1);
QCOMPARE(treeView->columnAtIndex(QModelIndex()), -1);
}
@@ -390,6 +393,48 @@ void tst_qquicktreeview::insertRows()
QCOMPARE(treeView->rows(), 9);
}
+void tst_qquicktreeview::insertColumns()
+{
+ // Check that if we add new columns to the model, TreeView gets updated
+ // to contain the new expected number of rows (flattened to a list)
+ LOAD_TREEVIEW("normaltreeview.qml");
+ treeView->expand(0);
+ WAIT_UNTIL_POLISHED;
+
+ QCOMPARE(treeView->columns(), 5);
+
+ const QModelIndex rootNode = model->index(0, 0, QModelIndex());
+ model->insertColumns(0, 2, rootNode);
+ WAIT_UNTIL_POLISHED;
+
+ QCOMPARE(treeView->columns(), 7);
+ auto childItem1 = treeViewPrivate->loadedTableItem(QPoint(0, 1))->item;
+ QCOMPARE(childItem1->property("text").toString(), "0, 0 (inserted)");
+ auto childItem2 = treeViewPrivate->loadedTableItem(QPoint(0, 2))->item;
+ QCOMPARE(childItem2->property("text").toString(), "1, 0 (inserted)");
+ auto childItem3 = treeViewPrivate->loadedTableItem(QPoint(0, 3))->item;
+ QCOMPARE(childItem3->property("text").toString(), "2, 0 (inserted)");
+ auto childItem4 = treeViewPrivate->loadedTableItem(QPoint(3, 0))->item;
+ QCOMPARE(childItem4->property("text").toString(), "0, 1");
+ auto childItem5 = treeViewPrivate->loadedTableItem(QPoint(3, 1))->item;
+ QCOMPARE(childItem5->property("text").toString(), "0, 1");
+
+ const QModelIndex indexOfInsertedChild = model->index(1, 0, rootNode);
+ model->insertRows(0, 2, indexOfInsertedChild);
+ treeView->expand(2);
+ WAIT_UNTIL_POLISHED;
+
+ QCOMPARE(treeView->rows(), 7);
+ QCOMPARE(treeView->columns(), 7);
+
+ for (int i = 0; i < 7; i++) {
+ for (int j = 0; j < 7; j++) {
+ auto childItem = treeViewPrivate->loadedTableItem(QPoint(j, i))->item;
+ QVERIFY(childItem);
+ }
+ }
+}
+
void tst_qquicktreeview::expandChildPendingToBeVisible()
{
// Check that if we expand a row r1, and that row has a child r2 that can
@@ -434,7 +479,7 @@ void tst_qquicktreeview::expandRecursivelyRoot()
QFETCH(int, depth);
LOAD_TREEVIEW("normaltreeview.qml");
- QSignalSpy spy(treeView, SIGNAL(expanded(int, int)));
+ QSignalSpy spy(treeView, SIGNAL(expanded(int,int)));
treeView->expandRecursively(rowToExpand, depth);
@@ -461,7 +506,7 @@ void tst_qquicktreeview::expandRecursivelyRoot()
// Check that all rows after rowToExpand, that are also
// children of that row, is expanded (down to depth)
for (int currentRow = rowToExpand + 1; currentRow < treeView->rows(); ++currentRow) {
- const auto modelIndex = treeView->modelIndex(currentRow, 0);
+ const auto modelIndex = treeView->index(currentRow, 0);
const int currentDepth = treeView->depth(currentRow);
const bool isChild = currentDepth > rowToExpandDepth;
const bool isExpandable = model->rowCount(modelIndex) > 0;
@@ -491,7 +536,7 @@ void tst_qquicktreeview::expandRecursivelyChild()
QFETCH(int, depth);
LOAD_TREEVIEW("normaltreeview.qml");
- QSignalSpy spy(treeView, SIGNAL(expanded(int, int)));
+ QSignalSpy spy(treeView, SIGNAL(expanded(int,int)));
treeView->expand(0);
@@ -513,7 +558,7 @@ void tst_qquicktreeview::expandRecursivelyChild()
WAIT_UNTIL_POLISHED;
- const bool rowToExpandDepth = treeView->depth(rowToExpand);
+ const int rowToExpandDepth = treeView->depth(rowToExpand);
const int effectiveMaxDepth = depth != -1 ? rowToExpandDepth + depth : model->maxDepth();
// Check that none of the rows before rowToExpand are expanded
@@ -532,7 +577,7 @@ void tst_qquicktreeview::expandRecursivelyChild()
for (int currentRow = rowToExpand + 1; currentRow < treeView->rows(); ++currentRow) {
const int currentDepth = treeView->depth(currentRow);
const bool isChild = currentDepth > rowToExpandDepth;
- const auto modelIndex = treeView->modelIndex(currentRow, 0);
+ const auto modelIndex = treeView->index(currentRow, 0);
const bool isExpandable = model->rowCount(modelIndex) > 0;
const bool shouldBeExpanded = isChild && isExpandable && currentDepth < effectiveMaxDepth;
QCOMPARE(treeView->isExpanded(currentRow), shouldBeExpanded);
@@ -543,7 +588,7 @@ void tst_qquicktreeview::expandRecursivelyWholeTree()
{
// Check that we expand the whole tree recursively by passing -1, -1
LOAD_TREEVIEW("normaltreeview.qml");
- QSignalSpy spy(treeView, SIGNAL(expanded(int, int)));
+ QSignalSpy spy(treeView, SIGNAL(expanded(int,int)));
treeView->expandRecursively(-1, -1);
QCOMPARE(spy.size(), 1);
@@ -555,7 +600,7 @@ void tst_qquicktreeview::expandRecursivelyWholeTree()
// Check that all rows that have children are expanded
for (int currentRow = 0; currentRow < treeView->rows(); ++currentRow) {
- const auto modelIndex = treeView->modelIndex(currentRow, 0);
+ const auto modelIndex = treeView->index(currentRow, 0);
const bool isExpandable = model->rowCount(modelIndex) > 0;
QCOMPARE(treeView->isExpanded(currentRow), isExpandable);
}
@@ -575,7 +620,7 @@ void tst_qquicktreeview::collapseRecursivelyRoot()
const int expectedRowCount = 1 + (model->maxDepth() * 8) - 4;
QCOMPARE(treeView->rows(), expectedRowCount);
- QSignalSpy spy(treeView, SIGNAL(collapsed(int, bool)));
+ QSignalSpy spy(treeView, SIGNAL(collapsed(int,bool)));
// Collapse the whole tree again. This time, only the root should end up visible
treeView->collapseRecursively();
@@ -595,7 +640,7 @@ void tst_qquicktreeview::collapseRecursivelyRoot()
// We can do that by simply iterate over the rows in the view as we expand.
int currentRow = 0;
while (currentRow < treeView->rows()) {
- const QModelIndex currentIndex = treeView->modelIndex(currentRow, 0);
+ const QModelIndex currentIndex = treeView->index(currentRow, 0);
if (model->hasChildren(currentIndex)) {
QVERIFY(!treeView->isExpanded(currentRow));
treeView->expand(currentRow);
@@ -622,11 +667,11 @@ void tst_qquicktreeview::collapseRecursivelyChild()
const int expectedRowCount = 1 + (model->maxDepth() * 8) - 4;
QCOMPARE(treeView->rows(), expectedRowCount);
- QSignalSpy spy(treeView, SIGNAL(collapsed(int, bool)));
+ QSignalSpy spy(treeView, SIGNAL(collapsed(int,bool)));
// Collapse the 8th child recursive
const int rowToCollapse = 8;
- const QModelIndex collapseIndex = treeView->modelIndex(rowToCollapse, 0);
+ const QModelIndex collapseIndex = treeView->index(rowToCollapse, 0);
const auto expectedLabel = model->data(collapseIndex, Qt::DisplayRole);
QCOMPARE(expectedLabel, QStringLiteral("3, 0"));
treeView->collapseRecursively(rowToCollapse);
@@ -646,7 +691,7 @@ void tst_qquicktreeview::collapseRecursivelyChild()
// We can do that by simply iterate over the rows in the view as we expand.
int currentRow = 1; // start at first child
while (currentRow < treeView->rows()) {
- const QModelIndex currentIndex = treeView->modelIndex(currentRow, 0);
+ const QModelIndex currentIndex = treeView->index(currentRow, 0);
if (model->hasChildren(currentIndex)) {
if (treeView->depth(currentRow) == 1 && currentIndex.row() == 2) {
// We did only recursively expand the 4th child, so the
@@ -669,7 +714,7 @@ void tst_qquicktreeview::collapseRecursivelyWholeTree()
{
// Check that we collapse the whole tree recursively by passing -1
LOAD_TREEVIEW("normaltreeview.qml");
- QSignalSpy spy(treeView, SIGNAL(collapsed(int, bool)));
+ QSignalSpy spy(treeView, SIGNAL(collapsed(int,bool)));
treeView->expandRecursively();
treeView->collapseRecursively();
@@ -688,7 +733,7 @@ void tst_qquicktreeview::expandToIndex()
// Check that expandToIndex(index) expands the tree so
// that index becomes visible in the view
LOAD_TREEVIEW("normaltreeview.qml");
- QSignalSpy spy(treeView, SIGNAL(expanded(int, int)));
+ QSignalSpy spy(treeView, SIGNAL(expanded(int,int)));
const QModelIndex root = model->index(0, 0);
const QModelIndex child1 = model->index(3, 0, root);
@@ -731,7 +776,7 @@ void tst_qquicktreeview::toggleExpandedUsingArrowKeys()
QQuickWindow *window = treeView->window();
// Start by making cell 0, 0 current
- treeView->selectionModel()->setCurrentIndex(treeView->modelIndex(0, 0), QItemSelectionModel::NoUpdate);
+ treeView->selectionModel()->setCurrentIndex(treeView->index(0, 0), QItemSelectionModel::NoUpdate);
// Expand row 0
const int row0 = 0;
@@ -747,7 +792,7 @@ void tst_qquicktreeview::toggleExpandedUsingArrowKeys()
// Hitting Key_Right again should be a no-op
QTest::keyPress(window, Qt::Key_Right);
QVERIFY(treeView->isExpanded(row0));
- QCOMPARE(treeView->selectionModel()->currentIndex(), treeView->modelIndex(row0, 0));
+ QCOMPARE(treeView->selectionModel()->currentIndex(), treeView->index(row0, 0));
// Move down to row 1 and try to expand it. Since Row 1
// doesn't have children, expanding it will be a no-op.
@@ -758,7 +803,7 @@ void tst_qquicktreeview::toggleExpandedUsingArrowKeys()
QTest::keyPress(window, Qt::Key_Down);
QTest::keyPress(window, Qt::Key_Right);
QVERIFY(!treeView->isExpanded(row1));
- QCOMPARE(treeView->selectionModel()->currentIndex(), treeView->modelIndex(row1, 0));
+ QCOMPARE(treeView->selectionModel()->currentIndex(), treeView->index(row1, 0));
// Move down to row 4 and expand it
const int row4 = 4;
@@ -768,7 +813,7 @@ void tst_qquicktreeview::toggleExpandedUsingArrowKeys()
QVERIFY(!treeView->isExpanded(row4));
QTest::keyPress(window, Qt::Key_Right);
QVERIFY(treeView->isExpanded(row4));
- QCOMPARE(treeView->selectionModel()->currentIndex(), treeView->modelIndex(row4, 0));
+ QCOMPARE(treeView->selectionModel()->currentIndex(), treeView->index(row4, 0));
// Move up again to row 0 and collapse it
while (treeView->currentRow() != row0)
@@ -781,7 +826,7 @@ void tst_qquicktreeview::toggleExpandedUsingArrowKeys()
// Hitting Key_Left again should be a no-op
QTest::keyPress(window, Qt::Key_Left);
QVERIFY(!treeView->isExpanded(row0));
- QCOMPARE(treeView->selectionModel()->currentIndex(), treeView->modelIndex(row0, 0));
+ QCOMPARE(treeView->selectionModel()->currentIndex(), treeView->index(row0, 0));
}
void tst_qquicktreeview::expandAndCollapsUsingDoubleClick()
@@ -791,7 +836,7 @@ void tst_qquicktreeview::expandAndCollapsUsingDoubleClick()
QCOMPARE(treeViewPrivate->loadedRows.count(), 1);
// Expand the root by double clicking on the row
- const auto item = treeView->itemAtCell(0, 0);
+ const auto item = treeView->itemAtIndex(treeView->index(0, 0));
QVERIFY(item);
const QPoint localPos = QPoint(item->width() / 2, item->height() / 2);
const QPoint pos = item->window()->contentItem()->mapFromItem(item, localPos).toPoint();
@@ -876,6 +921,7 @@ void tst_qquicktreeview::selectionBehaviorCells()
const QPointF endPos(endItem->x(), endItem->y());
const QPointF endPosWrapped(endItemWrapped->x(), endItemWrapped->y());
+ QVERIFY(treeViewPrivate->startSelection(startPos, Qt::NoModifier));
treeViewPrivate->setSelectionStartPos(startPos);
treeViewPrivate->setSelectionEndPos(endPos);
@@ -888,7 +934,7 @@ void tst_qquicktreeview::selectionBehaviorCells()
for (int x = x1; x < x2; ++x) {
for (int y = y1; y < y2; ++y) {
- const auto index = treeView->modelIndex(y, x);
+ const auto index = treeView->index(y, x);
QVERIFY(selectionModel->isSelected(index));
}
}
@@ -930,6 +976,7 @@ void tst_qquicktreeview::selectionBehaviorRows()
QCOMPARE(selectionModel->hasSelection(), false);
// Drag from row 0 to row 3
+ QVERIFY(treeViewPrivate->startSelection(QPointF(0, 0), Qt::NoModifier));
treeViewPrivate->setSelectionStartPos(QPointF(0, 0));
treeViewPrivate->setSelectionEndPos(QPointF(80, 60));
@@ -941,7 +988,7 @@ void tst_qquicktreeview::selectionBehaviorRows()
for (int x = 0; x < treeView->columns(); ++x) {
for (int y = 0; y < 3; ++y) {
- const auto index = treeView->modelIndex(y, x);
+ const auto index = treeView->index(y, x);
QVERIFY(selectionModel->isSelected(index));
}
}
@@ -950,6 +997,7 @@ void tst_qquicktreeview::selectionBehaviorRows()
QCOMPARE(selectionModel->hasSelection(), false);
// Drag from row 3 to row 0 (and overshoot mouse)
+ QVERIFY(treeViewPrivate->startSelection(QPointF(80, 60), Qt::NoModifier));
treeViewPrivate->setSelectionStartPos(QPointF(80, 60));
treeViewPrivate->setSelectionEndPos(QPointF(-10, -10));
@@ -960,7 +1008,7 @@ void tst_qquicktreeview::selectionBehaviorRows()
for (int x = 0; x < treeView->columns(); ++x) {
for (int y = 0; y < 3; ++y) {
- const auto index = treeView->modelIndex(y, x);
+ const auto index = treeView->index(y, x);
QVERIFY(selectionModel->isSelected(index));
}
}
@@ -981,6 +1029,7 @@ void tst_qquicktreeview::selectionBehaviorColumns()
QCOMPARE(selectionModel->hasSelection(), false);
// Drag from column 0 to column 3
+ QVERIFY(treeViewPrivate->startSelection(QPointF(0, 0), Qt::NoModifier));
treeViewPrivate->setSelectionStartPos(QPointF(0, 0));
treeViewPrivate->setSelectionEndPos(QPointF(225, 90));
@@ -992,7 +1041,7 @@ void tst_qquicktreeview::selectionBehaviorColumns()
for (int x = 0; x < 3; ++x) {
for (int y = 0; y < treeView->rows(); ++y) {
- const auto index = treeView->modelIndex(y, x);
+ const auto index = treeView->index(y, x);
QVERIFY(selectionModel->isSelected(index));
}
}
@@ -1001,6 +1050,7 @@ void tst_qquicktreeview::selectionBehaviorColumns()
QCOMPARE(selectionModel->hasSelection(), false);
// Drag from column 3 to column 0 (and overshoot mouse)
+ QVERIFY(treeViewPrivate->startSelection(QPointF(225, 90), Qt::NoModifier));
treeViewPrivate->setSelectionStartPos(QPointF(225, 90));
treeViewPrivate->setSelectionEndPos(QPointF(-10, -10));
@@ -1011,7 +1061,7 @@ void tst_qquicktreeview::selectionBehaviorColumns()
for (int x = 0; x < 3; ++x) {
for (int y = 0; y < treeView->rows(); ++y) {
- const auto index = treeView->modelIndex(y, x);
+ const auto index = treeView->index(y, x);
QVERIFY(selectionModel->isSelected(index));
}
}
@@ -1030,10 +1080,11 @@ void tst_qquicktreeview::selectionBehaviorDisabled()
QCOMPARE(selectionModel->hasSelection(), false);
- // Drag from column 0 to column 3
- treeViewPrivate->setSelectionStartPos(QPointF(0, 0));
- treeViewPrivate->setSelectionEndPos(QPointF(60, 60));
-
+ // Try to start a selection. treeViewPrivate->startSelection() should
+ // reject that, and and return false. The selectionFlag will there stay as
+ // QItemSelectionModel::NoUpdate, meaning no active selection is ongoing.
+ QVERIFY(!treeViewPrivate->startSelection(QPointF(0, 0), Qt::NoModifier));
+ QCOMPARE(treeViewPrivate->selectionFlag, QItemSelectionModel::NoUpdate);
QCOMPARE(selectionModel->hasSelection(), false);
}
@@ -1068,7 +1119,7 @@ void tst_qquicktreeview::sortTreeModel()
// is the same as in the view. That means that QQmlTreeModelToTableModel
// and QSortFilterProxyModel are in sync.
for (int row = 0; row < treeView->rows(); ++row) {
- const auto index = treeView->modelIndex(row, 0);
+ const auto index = treeView->index(row, 0);
const QString modelDisplay = proxyModel.data(index, Qt::DisplayRole).toString();
const auto childFxItem = treeViewPrivate->loadedTableItem(QPoint(0, row));
QVERIFY(childFxItem);
@@ -1084,7 +1135,7 @@ void tst_qquicktreeview::sortTreeModel()
WAIT_UNTIL_POLISHED;
for (int row = 0; row < treeView->rows(); ++row) {
- const auto index = treeView->modelIndex(row, 0);
+ const auto index = treeView->index(row, 0);
const QString modelDisplay = proxyModel.data(index, Qt::DisplayRole).toString();
const auto childFxItem = treeViewPrivate->loadedTableItem(QPoint(0, row));
QVERIFY(childFxItem);
@@ -1133,7 +1184,7 @@ void tst_qquicktreeview::sortTreeModelDynamic()
// is the same as in the view. That means that QQmlTreeModelToTableModel
// and QSortFilterProxyModel are in sync.
for (int row = 0; row < treeView->rows(); ++row) {
- const auto index = treeView->modelIndex(row, 0);
+ const auto index = treeView->index(row, 0);
const QString modelDisplay = proxyModel.data(index, Qt::DisplayRole).toString();
const auto childFxItem = treeViewPrivate->loadedTableItem(QPoint(0, row));
QVERIFY(childFxItem);
@@ -1146,10 +1197,10 @@ void tst_qquicktreeview::sortTreeModelDynamic()
// Now change the text in one of the items. This will trigger
// a sort for only one of the parents in the model.
- proxyModel.setData(treeView->modelIndex(row, 0), u"xxx"_s, Qt::DisplayRole);
+ proxyModel.setData(treeView->index(row, 0), u"xxx"_s, Qt::DisplayRole);
for (int row = 0; row < treeView->rows(); ++row) {
- const auto index = treeView->modelIndex(row, 0);
+ const auto index = treeView->index(row, 0);
const QString modelDisplay = proxyModel.data(index, Qt::DisplayRole).toString();
const auto childFxItem = treeViewPrivate->loadedTableItem(QPoint(0, row));
QVERIFY(childFxItem);
@@ -1161,6 +1212,100 @@ void tst_qquicktreeview::sortTreeModelDynamic()
}
}
+void tst_qquicktreeview::setRootIndex()
+{
+ // Check that if you can change the root index in the view to point
+ // at a child branch in the model
+ LOAD_TREEVIEW("normaltreeview.qml");
+
+ const QModelIndex rootIndex = model->index(0, 0);
+ const QModelIndex childIndex = model->index(3, 0, rootIndex);
+ QVERIFY(model->hasChildren(childIndex));
+ treeView->setRootIndex(childIndex);
+
+ // Go through all rows in the view, and check that view shows the
+ // same display text as the display role in the model (under the
+ // given root).
+ for (int row = 0; row < treeView->rows(); ++row) {
+ const auto index = model->index(row, 0, childIndex);
+ const QString modelDisplay = model->data(index, Qt::DisplayRole).toString();
+ const auto childFxItem = treeViewPrivate->loadedTableItem(QPoint(0, row));
+ QVERIFY(childFxItem);
+ const auto childItem = childFxItem->item;
+ QVERIFY(childItem);
+ const auto context = qmlContext(childItem.data());
+ const auto itemDisplay = context->contextProperty("display").toString();
+ QCOMPARE(itemDisplay, modelDisplay);
+ }
+
+ // Do the same once more, but this time choose a child that is deeper in the model
+ const QModelIndex childIndex2 = model->index(3, 0, childIndex);
+ QVERIFY(model->hasChildren(childIndex2));
+ treeView->setRootIndex(childIndex);
+
+ for (int row = 0; row < treeView->rows(); ++row) {
+ const auto index = model->index(row, 0, childIndex2);
+ const QString modelDisplay = model->data(index, Qt::DisplayRole).toString();
+ const auto childFxItem = treeViewPrivate->loadedTableItem(QPoint(0, row));
+ QVERIFY(childFxItem);
+ const auto childItem = childFxItem->item;
+ QVERIFY(childItem);
+ const auto context = qmlContext(childItem.data());
+ const auto itemDisplay = context->contextProperty("display").toString();
+ QCOMPARE(itemDisplay, modelDisplay);
+ }
+
+ // Reset rootIndex. This should show the whole model again
+ treeView->setRootIndex(QModelIndex());
+
+ for (int row = 0; row < treeView->rows(); ++row) {
+ const auto index = model->index(row, 0);
+ const QString modelDisplay = model->data(index, Qt::DisplayRole).toString();
+ const auto childFxItem = treeViewPrivate->loadedTableItem(QPoint(0, row));
+ QVERIFY(childFxItem);
+ const auto childItem = childFxItem->item;
+ QVERIFY(childItem);
+ const auto context = qmlContext(childItem.data());
+ const auto itemDisplay = context->contextProperty("display").toString();
+ QCOMPARE(itemDisplay, modelDisplay);
+ }
+}
+
+void tst_qquicktreeview::setRootIndexToLeaf()
+{
+ // When you set a custom root index, the root index itself will not
+ // be shown. Therefore, check that if you change the root index to a
+ // leaf in the model, TreeView will be empty.
+ LOAD_TREEVIEW("normaltreeview.qml");
+
+ const QModelIndex rootIndex = model->index(0, 0);
+ const QModelIndex leafIndex = model->index(1, 0, rootIndex);
+ QVERIFY(!model->hasChildren(leafIndex));
+ treeView->setRootIndex(leafIndex);
+ WAIT_UNTIL_POLISHED;
+ QCOMPARE(treeView->rows(), 0);
+
+ // According to the docs, you can set rootIndex to undefined
+ // in order to show the whole model again. This is the same
+ // as calling 'reset' on the property from c++. Verify that this works.
+ const QMetaObject *metaObject = treeView->metaObject();
+ const int propertyIndex = metaObject->indexOfProperty("rootIndex");
+ QVERIFY(propertyIndex != -1);
+ metaObject->property(propertyIndex).reset(treeView);
+
+ for (int row = 0; row < treeView->rows(); ++row) {
+ const auto index = model->index(row, 0);
+ const QString modelDisplay = model->data(index, Qt::DisplayRole).toString();
+ const auto childFxItem = treeViewPrivate->loadedTableItem(QPoint(0, row));
+ QVERIFY(childFxItem);
+ const auto childItem = childFxItem->item;
+ QVERIFY(childItem);
+ const auto context = qmlContext(childItem.data());
+ const auto itemDisplay = context->contextProperty("display").toString();
+ QCOMPARE(itemDisplay, modelDisplay);
+ }
+}
+
QTEST_MAIN(tst_qquicktreeview)
#include "tst_qquicktreeview.moc"