PDA

View Full Version : model-view framework and canFetchmore/fetchMore



tuli
14th August 2015, 22:39
I have a huge vector (100+K elements) with elements that I want to show to the user in a QTableView. To do this I am using a custom model to serve the vector and a filter model. The user can then filter the data, which is often the first thing the user does.

Now, whenever the user "filters" by an empty string or also when the thing starts up for the first time - then there is a long delay where the model is serving all the 100+K items to the view.


Since the user is never interested in looking at all the 100,000 items, it would make sens to only show the first few hundred to the user and fetch more if and as needed. There appears to be a mechanism in the Qt model/view framework to do just that: canFetchMore() and fetchMore().



however, I do not understand how these work in conjunction with a linear data structure like a vector. There is a tree based example which fetches data when the user expands a node - but I dont understand how that might work for a vector or a datastructure without such a clear cut parent-child/visible-invisible relation.

d_stranz
14th August 2015, 22:46
The problem you are facing has been discussed at length in another post, and is due to the fact that the view must determine the size of every item before it can set the scrollbar thumb to the right size and position. Even if it displays only a fraction of the rows, it still must ask each one for its size.

I suppose the only way around this is to have your model lie when asked for the row count if no filtering has been applied. The scollbar won't be the right size, but if you don't anticipate that the user will scroll through 100K entries before filtering, then it won't matter so much.

tuli
14th August 2015, 22:54
Why must it do that? All items have the same height. Can I set the scrollbar manually somehow?


Do you have a link to that other post, I cant find it.

But wait, the QSqlQuery model does essentially the same thing - and I just saw that here everything works fine. The scrollbar is bigger, and when the user scrolls down, it loads more items and the scrollbar becomes smaller!


Looking at the source I do not fully understand how it does that right now though.

d_stranz
14th August 2015, 23:21
Do you have a link to that other post, I cant find it.

It's this one, I think. (http://www.qtcentre.org/threads/63038) It's a tree view and rowCount(), but it's the same idea.

I don't know how the QSqlQueryModel does this magic. But if you read the docs for canFetchMore() for this class, it does say that it applies only for databases that don't report the size of the result set returned by a query.

I guess the thing to do is to override this method in your model and see where / when it is called and modify what it does to better fit the performance you need.

anda_skoa
14th August 2015, 23:53
My guess is that this "filter" is a QSortFilterProxyModel.
So it has to get all source data before it knows how many rows it has.

That's why one only uses QSortFilterProxyModel if you can't change the source model to do it there, or if the convenience of not doing it yourself is viable.

Cheers,
_