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. where to call beginRemoveRows() when QAbstractItemModel is deleted from somewhere else
Forum Updated to NodeBB v4.3 + New Features

where to call beginRemoveRows() when QAbstractItemModel is deleted from somewhere else

Scheduled Pinned Locked Moved Unsolved General and Desktop
qabstractitemmobeginremoverows
6 Posts 3 Posters 2.0k Views 1 Watching
  • 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.
  • gde23G Offline
    gde23G Offline
    gde23
    wrote on last edited by
    #1

    Hello,

    I have a model view that derives from QAbstractItemModel.
    The items in the view are a list of open windows and derive from QMainWindow as well as my item class for the modelview.

    to close one of those the windows, the item deletes itself from the model, however since I do not interact with the ItemModel/ItemView directly I don't know where to call beginRemoveRows() endRemoveRows().

    Any ideas?

    VRoninV 1 Reply Last reply
    0
    • gde23G gde23

      Hello,

      I have a model view that derives from QAbstractItemModel.
      The items in the view are a list of open windows and derive from QMainWindow as well as my item class for the modelview.

      to close one of those the windows, the item deletes itself from the model, however since I do not interact with the ItemModel/ItemView directly I don't know where to call beginRemoveRows() endRemoveRows().

      Any ideas?

      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      @gde23 said in where to call beginRemoveRows() when QAbstractItemModel is deleted from somewhere else:

      the item deletes itself from the model

      How? Can you show the code?

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      0
      • gde23G Offline
        gde23G Offline
        gde23
        wrote on last edited by
        #3

        @VRonin
        Thanks for the fast answer.
        Here is the destructor of the item:

        AbstractPlot::~AbstractPlot()
        {   
            // remove item from parent-item (its a tree-model)
            if(m_parent_item != NULL)
            {
                for(size_t c=0; c<m_parent_item->getChildCount(); c++) {
                    if(m_parent_item->getChild(c) == this) {
                        m_parent_item->eraseChild(c);
                    }
                }
            }
            // delete other stuff
        }
        

        the eraseChild() method just removes the child from the vector containing the child items

        void Item::eraseChild(const size_t index)
        {
            m_child_items.erase(m_child_items.begin() + index);
        }
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Is it me or are you children items responsible for their own deletion from their parents ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • gde23G Offline
            gde23G Offline
            gde23
            wrote on last edited by gde23
            #5

            Hi,

            yes. At the moment it's the children being responsible for this. When the child destructor is called the item deletes the entry from the parent, but maybe it's a bad idea to implement it that way?

            EDIT:

            Ok i got it working somehow.

            I added two slots to the Model that have to be called from the item that wants to delete itself before it commits suicide.

            void MyModel::slotBeginRemoveItem(TreeItem* item)
            {
                QModelIndex ind = pointer2ModelIndex(item);
                QModelIndex parent = ind.parent();
                beginRemoveRows(parent, ind.row(), ind.row());
            }
            
            void MyModel::slotEndRemoveItem()
            {
                endRemoveRows();
            }
            

            The destructor of the item now loks like this

            TreeItem::~TreeItem()
            {
                emit signalBeginRemoveItem(static_cast<TreeItem*>(this));
                if(getParentItem() != NULL)
                {
                    getParentItem()->eraseChild(this);
                }
                emit signalEndRemoveItem();
                // other delete stuff
            }
            
            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Not a good idea indeed. Why would the item have to know anything about what stores it ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2

              • Login

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