Results 1 to 2 of 2

Thread: Subclassing QAbstractItemModel for tree-like model: when to call e.g. beginAddRows

  1. #1
    Join Date
    Jan 2006
    Location
    Genk, Belgium
    Posts
    36
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Subclassing QAbstractItemModel for tree-like model: when to call e.g. beginAddRows

    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.
    Last edited by Pieter from Belgium; 6th July 2007 at 13:18. Reason: updated contents

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Subclassing QAbstractItemModel for tree-like model: when to call e.g. beginAddRow

    Quote Originally Posted by Pieter from Belgium View Post
    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.