Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] beginRemoveRows() with an unknown amount of rows

[Solved] beginRemoveRows() with an unknown amount of rows

Scheduled Pinned Locked Moved General and Desktop
qabstractitemmo
5 Posts 2 Posters 5.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Joel Bodenmann
    wrote on 29 Aug 2015, 11:32 last edited by Joel Bodenmann
    #1

    I have a custom subclass of QAbstractItemModel which I use for my QTreeView. I want to remove a (root) item of the tree. Therefore I call delete rootItem;. As this modifies the model data I would have to wrap that call inside of beginRemoveRows() and endRemoveRows(). However, inside the destructor of the rootItem all children will be deleted recursively. Therefore, I don't know how many items will be removed (nor do I know the parent) and therefore I don't know which parameters I have to pass to beginRemoveRows().

    How do I solve this issue?

    Industrial process automation software: https://simulton.com
    Embedded Graphics & GUI library: https://ugfx.io

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 29 Aug 2015, 14:49 last edited by
      #2

      You shouldn't remove the root. Root is the invisible item that all the top level items are children of.

      If you want to remove a single item( with QModeIndex index) from its parent (index.parent()) then you call beginRemoveRows(index.parent(), index.row(), index.row()) i.e. you remove a single row. Children of the index that is removed is not counted here.

      J 1 Reply Last reply 29 Aug 2015, 15:01
      0
      • C Chris Kawa
        29 Aug 2015, 14:49

        You shouldn't remove the root. Root is the invisible item that all the top level items are children of.

        If you want to remove a single item( with QModeIndex index) from its parent (index.parent()) then you call beginRemoveRows(index.parent(), index.row(), index.row()) i.e. you remove a single row. Children of the index that is removed is not counted here.

        J Offline
        J Offline
        Joel Bodenmann
        wrote on 29 Aug 2015, 15:01 last edited by
        #3

        @Chris-Kawa Sorry for being unclear. I don't want to remove the invisible root item but one of the top level items (a root item for the user, one with no parent).
        Anyway, this doesn't really matter in my case. When I want to delete any item in my tree then it will automatically delete all it's children recursively in its destructor, no matter on which level it is. So I don't want to remove a single either either.

        Deleting all the children in an items destructor is working, all I need to know is figure out what to pass to beginRemoveRows() before calling delete on the "parent item".

        Industrial process automation software: https://simulton.com
        Embedded Graphics & GUI library: https://ugfx.io

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on 29 Aug 2015, 16:13 last edited by
          #4

          That's what I'm saying. If you wan't to remove an item you don't have to worry about its children - you don't have to consider them at all in the call to beginRemoveRows().

          Lets say you have this tree:

          item1
          item2
             subitem1
                subsubitem1
                subsubitem1
             subitem2
          item3
          item4
          

          and lets say you want to remove "item2" with all children. This is how to do it:

          QModelIndex index = /*  get the index of "item2"  */
          beginRemoveRows(index.parent(), index.row(), index.row());
          ...// physically remove the item from your data structure along with all the children
          endRemoveRows();
          
          J 1 Reply Last reply 29 Aug 2015, 16:53
          1
          • C Chris Kawa
            29 Aug 2015, 16:13

            That's what I'm saying. If you wan't to remove an item you don't have to worry about its children - you don't have to consider them at all in the call to beginRemoveRows().

            Lets say you have this tree:

            item1
            item2
               subitem1
                  subsubitem1
                  subsubitem1
               subitem2
            item3
            item4
            

            and lets say you want to remove "item2" with all children. This is how to do it:

            QModelIndex index = /*  get the index of "item2"  */
            beginRemoveRows(index.parent(), index.row(), index.row());
            ...// physically remove the item from your data structure along with all the children
            endRemoveRows();
            
            J Offline
            J Offline
            Joel Bodenmann
            wrote on 29 Aug 2015, 16:53 last edited by
            #5

            @Chris-Kawa Oh, now i understand. Well, this makes a lot of sense.

            Thank you for your help, very appreciated!

            Industrial process automation software: https://simulton.com
            Embedded Graphics & GUI library: https://ugfx.io

            1 Reply Last reply
            0

            4/5

            29 Aug 2015, 16:13

            • Login

            • Login or register to search.
            4 out of 5
            • First post
              4/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved