[Interest] QAbstractItemModel and lazy loading

Adam Light aclight at gmail.com
Fri May 11 20:25:54 CEST 2012


Hi

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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20120511/2ba76f81/attachment.html>


More information about the Interest mailing list