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. how to delete single item from QAbstractTableModel?
Forum Updated to NodeBB v4.3 + New Features

how to delete single item from QAbstractTableModel?

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 1.7k 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.
  • J Offline
    J Offline
    johnby
    wrote on last edited by
    #1

    I am trying to remove a single row and have overridden removeRow() in MyModel.

    class MyModel : public QAbstractTableModel
    {
        QList<MyObject> objects;
    
        public:
            // ...
        
            bool removeRow(int row, const QModelIndex &parent = QModelIndex());
    };
    

    The QAbstractItemModel::removeRow(...) docs say this.

    This is a convenience function that calls removeRows(). 
    

    And so in removeRow() I have the following, but I'm not sure how this should work. Do I have to call removeRows()?

    bool MyModel::removeRow(int row, const QModelIndex &parent)
    {
        beginRemoveRows(parent, first, row); ????
    
        objects.removeAt(row);
    
        endRemoveRows();
    
        return true;
    }
    
    1 Reply Last reply
    0
    • nageshN Offline
      nageshN Offline
      nagesh
      wrote on last edited by
      #2

      In beginRemoveRows function second and third argument should be same, as it's deleting single row.

      beginRemoveRows(parent, row, row);
      

      Application should call removeRow function whenever the particular row to be deleted.
      Refer addressbook program provided in the Qt Examples

      1 Reply Last reply
      1
      • jeremy_kJ Offline
        jeremy_kJ Offline
        jeremy_k
        wrote on last edited by jeremy_k
        #3

        Override QAbstractItemModel::removeRows rather than QAbstractItemModel::removeRow.
        The function signatures are:

        virtual bool removeRows(int row, int count, const QModelIndex &parent);
        inline bool removeRow(int row, const QModelIndex &parent);
        

        Note that removeRows is virtual, but removeRow is inline.

        Asking a question about code? http://eel.is/iso-c++/testcase/

        JonBJ 1 Reply Last reply
        2
        • jeremy_kJ jeremy_k

          Override QAbstractItemModel::removeRows rather than QAbstractItemModel::removeRow.
          The function signatures are:

          virtual bool removeRows(int row, int count, const QModelIndex &parent);
          inline bool removeRow(int row, const QModelIndex &parent);
          

          Note that removeRows is virtual, but removeRow is inline.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @jeremy_k
          I guess that is the case so that you are free to write your own removeRows() to remove multiple rows in a more efficient fashion than calling removeRow() multiple times, if that suits your impementation?

          jeremy_kJ 1 Reply Last reply
          0
          • JonBJ JonB

            @jeremy_k
            I guess that is the case so that you are free to write your own removeRows() to remove multiple rows in a more efficient fashion than calling removeRow() multiple times, if that suits your impementation?

            jeremy_kJ Offline
            jeremy_kJ Offline
            jeremy_k
            wrote on last edited by jeremy_k
            #5

            I would phrase it must rather than are free to if either removeRow or removeRows are used via a QAbstractItemModel pointer.

            Otherwise:

            inline bool QAbstractItemModel::removeRow(int arow, const QModelIndex &aparent)
            { return removeRows(arow, 1, aparent); }
            

            Which leads to:

            bool QAbstractItemModel::removeRows(int, int, const QModelIndex &)
            {
                return false;
            }
            

            Asking a question about code? http://eel.is/iso-c++/testcase/

            JonBJ 1 Reply Last reply
            1
            • jeremy_kJ jeremy_k

              I would phrase it must rather than are free to if either removeRow or removeRows are used via a QAbstractItemModel pointer.

              Otherwise:

              inline bool QAbstractItemModel::removeRow(int arow, const QModelIndex &aparent)
              { return removeRows(arow, 1, aparent); }
              

              Which leads to:

              bool QAbstractItemModel::removeRows(int, int, const QModelIndex &)
              {
                  return false;
              }
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @jeremy_k
              Yes indeed! I didn't look, I had assumed removeRows() called removeRow() repeatedly, when in fact it is removeRow() which calls removeRows() once!

              1 Reply Last reply
              0
              • J Offline
                J Offline
                johnby
                wrote on last edited by
                #7

                I noticed you guys didn't write

                beginRemoveRows(...);
                // ...
                endRemoveRows();
                

                Are these not necessary within the removeRow(s) calls?

                jeremy_kJ 1 Reply Last reply
                0
                • J johnby

                  I noticed you guys didn't write

                  beginRemoveRows(...);
                  // ...
                  endRemoveRows();
                  

                  Are these not necessary within the removeRow(s) calls?

                  jeremy_kJ Offline
                  jeremy_kJ Offline
                  jeremy_k
                  wrote on last edited by
                  #8

                  @johnby said in how to delete single item from QAbstractTableModel?:

                  I noticed you guys didn't write

                  beginRemoveRows(...);
                  // ...
                  endRemoveRows();
                  

                  Are these not necessary within the removeRow(s) calls?

                  They are necessary, and they should be used in the implementation of removeRows().

                  Asking a question about code? http://eel.is/iso-c++/testcase/

                  1 Reply Last reply
                  1

                  • Login

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