summaryrefslogtreecommitdiffstats
path: root/src/widgets/itemviews
diff options
context:
space:
mode:
authorVolker Hilsheimer <volker.hilsheimer@qt.io>2020-09-22 14:40:19 +0200
committerVolker Hilsheimer <volker.hilsheimer@qt.io>2020-10-02 13:16:12 +0200
commite74af68654c0eb127277c73e20bda409b83d157b (patch)
tree4a30a481c8b1aa711551c12ee1b8953435b81408 /src/widgets/itemviews
parent9b35a16c645cfbed80b23b81c30317f39a94e0dd (diff)
QTreeView: fetch as many nested entries as fit into the view
QAbstractItemModel::canFetchMore and fetchMore implementations might only add a few rows to the model each time they are called. The item views don't generally expect that, and don't call fetchMore repeatedly, even if there would be room in the itemview for more data. This problem cannot be generally solved for all item views, as it would require in repeated expensive laying out of items. For nested indexes in a treeview however, we can try to fetch enough child rows to populate the screen when the item is laid out by repeatedly calling canFetchMore and fetchMore. To calculate how many items have space, apply the same heuristics as in the scrollContentsBy implementation to guess the number of items that can fit into the viewport. Created test case for the fix. Done-with: Doris Verria <doris.verria@qt.io> Fixes: QTBUG-85366 Pick-to: 5.15 Change-Id: I54f95552993873dd4cba80b0f70f4db9d98ddc1d Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io> Reviewed-by: Andy Shaw <andy.shaw@qt.io> Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
Diffstat (limited to 'src/widgets/itemviews')
-rw-r--r--src/widgets/itemviews/qtreeview.cpp12
1 files changed, 10 insertions, 2 deletions
diff --git a/src/widgets/itemviews/qtreeview.cpp b/src/widgets/itemviews/qtreeview.cpp
index dedc27501f..3fd0da37cf 100644
--- a/src/widgets/itemviews/qtreeview.cpp
+++ b/src/widgets/itemviews/qtreeview.cpp
@@ -3342,9 +3342,17 @@ void QTreeViewPrivate::layout(int i, bool recursiveExpanding, bool afterIsUninit
int count = 0;
if (model->hasChildren(parent)) {
- if (model->canFetchMore(parent))
+ if (model->canFetchMore(parent)) {
+ // fetchMore first, otherwise we might not yet have any data for sizeHintForRow
model->fetchMore(parent);
- count = model->rowCount(parent);
+ // guestimate the number of items in the viewport, and fetch as many as might fit
+ const int itemHeight = defaultItemHeight <= 0 ? q->sizeHintForRow(0) : defaultItemHeight;
+ const int viewCount = viewport->height() / itemHeight;
+ while ((count = model->rowCount(parent)) < viewCount && model->canFetchMore(parent))
+ model->fetchMore(parent);
+ } else {
+ count = model->rowCount(parent);
+ }
}
bool expanding = true;