PDA

View Full Version : Speed Up TreeView



abbapatris
14th June 2010, 18:00
I have a QAbstractItemModel that interacts with a QTreeView. Currently it takes to long to load the tree and to scroll through the tree. Is there a way to speed up the tree?

Description of the tree:
1) Child nodes under root < 100
2) Each child node might have as many as 40 branches
3) The expand all function needs to be able to be applied
4) Fast scrolling is required.

We have tried several things to speed it up and haven't been able to get anywhere. My most recent attempt was to try to only have the view load what it can display in the viewport. I haven't been able to get that to work yet and I am on a deadline. Does anyone have any suggestions or obviously if you need more details let me know. Any help would be appreciated.

squidge
14th June 2010, 18:28
Did you try the MVC (model, view, controller) approach? That way you only provide data as necessary.

tbscope
14th June 2010, 19:08
How do you populate the tree?
Can you show some source code?

Avoid painting all the items. Only paint those that are on the screen.

abbapatris
14th June 2010, 19:12
I wish I could show you the code, but I legally can't. We are doing the model view approach, I don't know what the controller is though.

Currently there are 2 things that are really slow, loading the tree when you initially start up and also doing an expand and collapse all. I am not sure if the paintevent will help with these.

tbscope
14th June 2010, 19:28
I see you are using an abstract item model. This is the most important place to look for slowness.
The view doesn't know anything about the data so the model tells the view what to do when more items are available.

In plain English this means this:
1. When you use an abstract item model and don't do some magic, it will be very slow when you add thousands of items because the model tells the view that it got thousands of new items to paint. The view, which is not very clever by its own, will happily paint all of them. A huge waste of resources as you see only a fraction of the items.
2. When an item has even more child items, the tree view is a little bit smarter, it doesn't paint those child items. But, what happens if you do an expand all? All those child items get painted too resulting in a huge loss of resources.

What you need to do in your model is tell the view to stop painting all the items.
When you open a child item, the tree view calls canFetchMore(...) of the model. The model tells the view that it has more items to paint.

You can reimplement the canFetchMore() and fetchMore() functions.

Take a look at the QSqlQueryModel for example.

ps: The controller, in Qt called the delegate, sits in between the model and the view. It tells the view how to draw certain items of the model and it tells the model how to update the data when something happens in the view (like an editor for example).

abbapatris
14th June 2010, 19:32
Alright, I will try that thanks

tbscope
14th June 2010, 19:42
Ohh, and if you want to try something easier right away, and this will help:
Do not add all your items at once to the model, add them incrementally using a timer for example.