[Interest] QAbstractItemModel and lazy loading

Jason Dictos jdictos at barracuda.com
Fri May 11 21:15:06 CEST 2012

What I've done to work around this is to always have a dummy item called "Loading..." as the first item in the list. Then when that node expands, load and replace it. That has turned out to be a pretty good user experience as well.

There doesn't seem to be a way to control the decoration of items in a tree view (I could be wrong however).


From: interest-bounces+jdictos=barracuda.com at qt-project.org [mailto:interest-bounces+jdictos=barracuda.com at qt-project.org] On Behalf Of Adam Light
Sent: Friday, May 11, 2012 2:26 PM
To: Interest at qt-project.org
Subject: [Interest] QAbstractItemModel and lazy loading


I have a QAbstractItemModel subclass SourceModel in my application. I have a QTreeView widget that displays SourceModel.

SourceModel monitors data structures in my application for changes and when it detects changes it updates itself to reflect those changes.

SourceModel is written to lazily load all children because loading children from the underlying data structures in my application can be slow.

In some cases a node in the tree (and the underlying data that is separate from the model) initially starts out having no children. Later, that node may get one or more children added to it. When my SourceModel notices that the underlying data has changed, it updates itself to reflect that change. In many cases, that simply means that the SourceModel::hasChildren() method will return true for that node and that SourceModel::canFetchMore() will also return true.

My problem is that when a node goes from having no children to having children (which have not yet been loaded), that item in the view does not get the expansion triangle drawn, so the user has no way of knowing that a particular node now has children.

I have tried emitting SourceModel::dataChanged() when a node goes from not having children to having children, but that does not cause the view to update correctly. Emitting layoutAboutToBeChanged()/layoutChanged() *does* cause the view to be updated correctly, but often I get crashes when layoutChanged() is called (possibly related to https://bugreports.qt-project.org/browse/QTBUG-19261).

What is my model supposed to do when a node in the model now has (unloaded) children to alert views using the model of the change? I can't use beginInsertRows() because the child rows are not yet actually inserted into the model. Emitting dataChanged() doesn't seem to have the desired effect.

I have confirmed that the hasChildren() and canFetchMore() methods of SourceModel are returning the correct results. Interestingly, hasChildren() is even called as a result of my model emitting the dataChanged() signal for the node that now has (unloaded) children.

Thanks for any help
Adam Light

'Like' us on Facebook for exclusive content and other resources on all Barracuda Networks solutions.
Visit http://barracudanetworks.com/facebook

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20120511/a148f4b1/attachment.html>

More information about the Interest mailing list