aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard Moe Gustavsen <richard.gustavsen@qt.io>2022-01-20 15:51:00 +0100
committerRichard Moe Gustavsen <richard.gustavsen@qt.io>2022-01-24 14:46:02 +0100
commitc67c468ec13a134c5ad7fe36bf69f12a3498a01b (patch)
tree7a62480d14db1464ed1264eb6785d38791ecd647
parentc89e24dd03d470ebda75ac34fc9de8ddb8c0c760 (diff)
QQuickTreeView: be able to expand rows that are not yet visible in the viewHEADmaster
If the app expands a row in TreeView, that row will for a brief moment be expanded in the proxy model, but not in the view, until the view is polished. For that reason, when determining if a row can be expanded or not, we should check the state of the proxy model, and not the view. Note: if the application needs the state of the view to reflect the state of the proxy model immediataly after a call to expand, forcePolish() is always possible. Task-number: QTBUG-91374 Change-Id: Ibda8d90e2dc2077a6f24090566ce3ba20a734c29 Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
-rw-r--r--src/qquicktreeview.cpp24
-rw-r--r--tests/auto/tst_treeview.cpp23
2 files changed, 37 insertions, 10 deletions
diff --git a/src/qquicktreeview.cpp b/src/qquicktreeview.cpp
index c2bd912..01010e0 100644
--- a/src/qquicktreeview.cpp
+++ b/src/qquicktreeview.cpp
@@ -647,40 +647,44 @@ QQuickTreeView::~QQuickTreeView()
bool QQuickTreeView::isExpanded(int row) const
{
- if (row < 0 || row >= rows())
+ Q_D(const QQuickTreeView);
+ if (row < 0 || row >= d->m_proxyModel.rowCount())
return false;
- return d_func()->m_proxyModel.isExpanded(row);
+ return d->m_proxyModel.isExpanded(row);
}
bool QQuickTreeView::hasChildren(int row) const
{
- if (row < 0 || row >= rows())
+ Q_D(const QQuickTreeView);
+ if (row < 0 || row >= d->m_proxyModel.rowCount())
return false;
- return d_func()->m_proxyModel.hasChildren(row);
+ return d->m_proxyModel.hasChildren(row);
}
bool QQuickTreeView::hasSiblings(int row) const
{
- if (row < 0 || row >= rows())
+ Q_D(const QQuickTreeView);
+ if (row < 0 || row >= d->m_proxyModel.rowCount())
return false;
- return d_func()->m_proxyModel.hasSiblings(row);
+ return d->m_proxyModel.hasSiblings(row);
}
int QQuickTreeView::depth(int row) const
{
- if (row < 0 || row >= rows())
+ Q_D(const QQuickTreeView);
+ if (row < 0 || row >= d->m_proxyModel.rowCount())
return -1;
- return d_func()->m_proxyModel.depthAtRow(row);
+ return d->m_proxyModel.depthAtRow(row);
}
void QQuickTreeView::expand(int row)
{
Q_D(QQuickTreeView);
- if (row < 0 || row >= rows())
+ if (row < 0 || row >= d->m_proxyModel.rowCount())
return;
if (d->m_proxyModel.isExpanded(row))
@@ -714,7 +718,7 @@ void QQuickTreeView::toggleModelIndexExpanded(const QModelIndex &modelIndex)
void QQuickTreeView::collapse(int row)
{
Q_D(QQuickTreeView);
- if (row < 0 || row >= rows())
+ if (row < 0 || row >= d->m_proxyModel.rowCount())
return;
if (!d->m_proxyModel.isExpanded(row))
diff --git a/tests/auto/tst_treeview.cpp b/tests/auto/tst_treeview.cpp
index 413b2b3..b8f1713 100644
--- a/tests/auto/tst_treeview.cpp
+++ b/tests/auto/tst_treeview.cpp
@@ -74,6 +74,7 @@ private slots:
void showTreeView();
void expandAndCollapseRoot();
void expandAndCollapseChildren();
+ void expandChildPendingToBeVisible();
void attachedPropertiesRoot();
void attachedPropertiesChildren();
void emptyModel();
@@ -167,6 +168,28 @@ void tst_treeview::expandAndCollapseChildren()
QCOMPARE(treeView->rows(), 1);
}
+void tst_treeview::expandChildPendingToBeVisible()
+{
+ // Check that if we expand a row r1, and that row has a child r2 that can
+ // be expanded, we can continue to expand c2 immediately, even if r1 is
+ // still pending to be shown as expanded in the view.
+ LOAD_TREEVIEW("normaltreeview.qml");
+ QMetaObject::invokeMethod(treeView, "expand", Q_ARG(int, 0));
+ // The view has not yet been updated at this point to show
+ // the newly expanded children, so it still has only one row.
+ QCOMPARE(treeView->rows(), 1);
+ // ...but we still expand row 5, which is a child that has children
+ // in the proxy model
+ QMetaObject::invokeMethod(treeView, "expand", Q_ARG(int, 4));
+ QCOMPARE(treeView->rows(), 1);
+
+ WAIT_UNTIL_POLISHED;
+
+ // Now the view have updated to show
+ // all the rows that has been expanded.
+ QCOMPARE(treeView->rows(), 9);
+}
+
void tst_treeview::attachedPropertiesRoot()
{
LOAD_TREEVIEW("normaltreeview.qml");