PDA

View Full Version : Subclassing QAbstractItemModel for tree-like model: when to call e.g. beginAddRows



Pieter from Belgium
6th July 2007, 11:47
When subclassing QAbstractItemModel for developing a tree model, it is unclear to me when the methods QAbstractItemModel::beginAddRows etc should be called, in particular when nested calls are concerned.

More concrete, suppose an item x is removed, along with its subitems, is it ok to only call beginRemoveRows and endRemoveRows for the item x, w.r.t. its removal from its parent item, or should these methods also be called for the subitems below x? Is it wrong to call these begin end methods for the sub items below x after the methods have been called for item x? Will that fail, or will it only be inefficient?

When removing toplevel items, can these methods be called with QModelIndex() as parent?

Likewise, for adding a hierarchy of items at once, is it harmful to call beginInsertRows for a subitem before the parent itself is beginInserted / endInserted?

I have the impression that these life cycle constraints and policies regarding the insertion/removal of a hierarchy of items at once are not clearly documented, or did I miss something.

wysota
6th July 2007, 12:16
When subclassing QAbstractItemModel for developing a tree model, it is unclear to me when the methods QAbstractItemModel::beginAddRows etc should be called, in particular when nested calls are concerned.
Ideally you'd call them from within your insertRow()/removeRow() implementation. So if you use insertRow(s) and removeRow(s), you shouldn't ever need to call those methods directly.


More concrete, suppose an item x is removed, along with its subitems, is it ok to only call beginRemoveRows and endRemoveRows for the item x, w.r.t. its removal from its parent item, or should these methods also be called for the subitems below x?
I think it's fine to call them for the top most level only.


Is it wrong to call these begin end methods for the sub items below x after the methods have been called for item x? Will that fail, or will it only be inefficient?
If you want to call them, you can call *begin() after parent's begin(), but before parent's end().


When removing toplevel items, can these methods be called with QModelIndex() as parent?
Yes.