Important: Please read the Qt Code of Conduct -

Control of QTreeView to create impression that all items are loaded, but actually only load them as needed to display.

  • I have a QTreeview that connects to a model that might contain many, many items to be shown. Literally millions. There is a significant delay in some situations, which I intend to get round by having the model not actually load the raw data unless the user wants to see it (which should make the model finish fetching data faster, and also do the QTreeView some favours by having only a small rowcount() and thus small amount of display housekeeping such as row height checking etc etc .)

    For example, if there are a million items, and the QTreeView is only showing the first twenty, there's no need to load all million into the model. If the model actually has the first thousand, it only needs to fetch the next thousand when the user scrolls down far enough, and at that point (allowing for some overlap etc) the original first thousand could even be discarded.

    So far so good. But, there is a need to create the impression for the user that all million items are available for viewing. What this really means is that I need the scroll bar at the side of the QTreeView to always act as if there are a million items in the QTreeView. So if the user drags the scroller to the bottom, I present the user with the last thousand items. For this reason, I can't use fetchMore() (I think) because that's about loading everything slowly, whereas I would in this case want to have the model just load the last thousand items. When the QTreeView asked for the rowcount, it would get told 1000, and would happily display the items, and should all be fine.

    So how can I have this level of control over the scroller? In essence, I'd want to scrollbar to look like there are the full million items available (so a very narrow little scroller dragger thingy), and if the user did drag the scroller, use that to work out which items need loading and displaying.

    I am sure that this is all very much a solved problem. Can anyone point me at an example?

  • Lifetime Qt Champion


    I don't know of an example to do that but basically your model should "lie" to your view so you need get the amount of data you have from your server and tell your view that you have e.g. 100'000 items in rowCount. Then in data you would probably have to do something like a detection of when you get "serial" calls to update the view and once it stops, check what indexes should be shown and only then fetch the data from your server. But that would also mean that your underlying that structure should be able to handle "holes" like for example if your user jumps from the top to the bottom of your view.

    Hope it helps

Log in to reply