From 764854dc4799c21f739abc35e68f053849267f89 Mon Sep 17 00:00:00 2001 From: Andy Shaw Date: Wed, 23 Nov 2016 11:09:47 +0100 Subject: [PATCH] Call canFetchMore/fetchMore when scrolled to the bottom of the view MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When scrolling to the bottom of the view, if there are expanded items then it should call canFetchMore/fetchMore up the chain until it finds one with more items that can possibly be retrieved. This brings it in line with the QAbstractItemView implementation which would call canFetchMore on the root index, therefore we go up the chain to the root to see if anything before that can be fetched. [ChangeLog][QtWidgets][QTreeView] QTreeView now calls canFetchMore and fetchMore when the bottom of the QTreeView is scrolled to. Task-number: QTBUG-48725 Change-Id: I2b2145684bb34c8c317bfce4a0ef14f243a64719 Reviewed-by: Thorbjørn Lund Martsum --- src/widgets/itemviews/qtreeview.cpp | 21 +++++++++ src/widgets/itemviews/qtreeview.h | 1 + .../itemviews/qtreeview/tst_qtreeview.cpp | 45 +++++++++++++++++++ 3 files changed, 67 insertions(+) diff --git a/src/widgets/itemviews/qtreeview.cpp b/src/widgets/itemviews/qtreeview.cpp index 61721143ef4..d504263bd12 100644 --- a/src/widgets/itemviews/qtreeview.cpp +++ b/src/widgets/itemviews/qtreeview.cpp @@ -3992,6 +3992,27 @@ int QTreeView::visualIndex(const QModelIndex &index) const return d->viewIndex(index); } +/*! + \reimp +*/ + +void QTreeView::verticalScrollbarValueChanged(int value) +{ + Q_D(QTreeView); + if (!d->viewItems.isEmpty() && value == verticalScrollBar()->maximum()) { + QModelIndex ret = d->viewItems.last().index; + // Root index will be handled by base class implementation + while (ret.isValid()) { + if (isExpanded(ret) && d->model->canFetchMore(ret)) { + d->model->fetchMore(ret); + break; + } + ret = ret.parent(); + } + } + QAbstractItemView::verticalScrollbarValueChanged(value); +} + QT_END_NAMESPACE #include "moc_qtreeview.cpp" diff --git a/src/widgets/itemviews/qtreeview.h b/src/widgets/itemviews/qtreeview.h index c32c127cd10..e9bc8189f8b 100644 --- a/src/widgets/itemviews/qtreeview.h +++ b/src/widgets/itemviews/qtreeview.h @@ -169,6 +169,7 @@ protected Q_SLOTS: void columnMoved(); void reexpand(); void rowsRemoved(const QModelIndex &parent, int first, int last); + void verticalScrollbarValueChanged(int value) Q_DECL_OVERRIDE; protected: QTreeView(QTreeViewPrivate &dd, QWidget *parent = Q_NULLPTR); diff --git a/tests/auto/widgets/itemviews/qtreeview/tst_qtreeview.cpp b/tests/auto/widgets/itemviews/qtreeview/tst_qtreeview.cpp index 45d33df356e..b25a423af62 100644 --- a/tests/auto/widgets/itemviews/qtreeview/tst_qtreeview.cpp +++ b/tests/auto/widgets/itemviews/qtreeview/tst_qtreeview.cpp @@ -171,6 +171,7 @@ private slots: void statusTip_data(); void statusTip(); + void fetchMoreOnScroll(); // task-specific tests: void task174627_moveLeftToRoot(); @@ -4470,5 +4471,49 @@ void tst_QTreeView::statusTip() QTest::mouseMove(mw.windowHandle(), centerPoint); QTRY_COMPARE(mw.statusBar()->currentMessage(), QLatin1String("Header 0 -- Status")); } + +class FetchMoreModel : public QStandardItemModel +{ +public: + FetchMoreModel() : QStandardItemModel(), canFetchReady(false) + { + for (int i = 0; i < 20; ++i) { + QStandardItem *item = new QStandardItem("Row"); + item->appendRow(new QStandardItem("Child")); + appendRow(item); + } + } + bool canFetchMore(const QModelIndex &parent) const override + { + if (!canFetchReady || !parent.isValid()) + return false; + if (!parent.parent().isValid()) + return rowCount(parent) < 20; + return false; + } + void fetchMore(const QModelIndex &parent) override + { + QStandardItem *item = itemFromIndex(parent); + for (int i = 0; i < 19; ++i) + item->appendRow(new QStandardItem(QString("New Child %1").arg(i))); + } + bool canFetchReady; +}; + +void tst_QTreeView::fetchMoreOnScroll() +{ + QTreeView tw; + FetchMoreModel im; + tw.setModel(&im); + tw.show(); + tw.expandAll(); + QTest::qWaitForWindowActive(&tw); + // Now we can allow the fetch to happen + im.canFetchReady = true; + tw.verticalScrollBar()->setValue(tw.verticalScrollBar()->maximum()); + // The item should have now fetched the other children, thus bringing the count to 20 + QCOMPARE(im.item(19)->rowCount(), 20); +} + QTEST_MAIN(tst_QTreeView) #include "tst_qtreeview.moc"