Is there a bug in my code for populating a QTreeView?
Question
I'm using PyQt 4.4.
It's best shown using some pictures. All nodes should have leafs from 0 to 99. They are being incrementally loaded using canFetchMore()
and fetchMore()
. But for some reason unknown for me this works only for the root node. (Picture 1)
If I collapse and expand a node it loads additional 10 values. (Picture 2 & 3)
It's also strange, that it loads 10 values, as the code loads only 5 per call to fetchMore()
, meaning this gets called 2 times before the code stops to load more data.
I've written a small example to demonstrate the problem, just run it with python test.py
.
http://snipt.org/lLh
Does anyone know what causes this error?
Solution
I took a look at the Qt source (v4.5, though I don't expect much difference between v4.4 and v4.5) for QAbstractItemView and QTreeView, and I don't think they support incremental lazy loading of child nodes.
QAbstractItemView has no notion of trees, so it only calls fetchMore()
on the top most index. It calls fetchMore()
when:
- Geometry is updated
- The scroll bars are moved
- Rows are inserted
- The current item is changed as a result of an autoscrolling drag & drop operation
QTreeView additionally calls fetchMore()
when:
- An item is expanded (this is essentially the only time it calls
fetchMore()
with a non-root index) - The view's layout needs to be relaid, such as with
expandAll()
andcollapseAll()
I think the best solution would be to subclass QTreeView to make it call fetchMore()
in the appropriate places and with the appropriate indices.