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. Adding rows to QAbstractTableModel
Forum Update on Monday, May 27th 2025

Adding rows to QAbstractTableModel

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 4.8k 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
    jss193
    wrote on 2 Jun 2019, 16:01 last edited by
    #1

    Hello,

    I have created a model such as:

    #include <QObject>
    #include <QAbstractTableModel>
    
    class ReactiveModelTable : public QAbstractTableModel
    {
       Q_OBJECT
    public:
        ReactiveModelTable(QObject *parent);
        int rowCount(const QModelIndex &parent)const;
        int columnCount(const QModelIndex &parent)const;
        QVariant data (const QModelIndex &index, int role)const;
    
    
    
    private:
        int rows=0;
        int columns=5;
    };
    
    #endif // REACTIVEMODELTABLE_H
    
    

    and

    #include "reactivemodeltable.h"
    
    ReactiveModelTable::ReactiveModelTable(QObject *parent):QAbstractTableModel (parent)
    {
    
    }
    
    int ReactiveModelTable::rowCount(const QModelIndex &parent) const
    {
        return rows;
    }
    
    int ReactiveModelTable::columnCount(const QModelIndex &parent) const
    {
        return columns;
    }
    
    
    
    
    QVariant ReactiveModelTable::data(const QModelIndex &index, int role) const
    {
        return QVariant();
    }
    
    

    Then in my file I start the model such as:
    modelo = new ReactiveModelTable(this);
    ui->tableView->setModel(modelo);

    then I have created a button and I want it to add a row, I tried the following code:
    modelo->insertRow(modelo->rowCount(QModelIndex()));
    ui->tableView->update();

    but it doesn't work...

    Any suggestion please?

    Thanks!

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 2 Jun 2019, 16:25 last edited by Chris Kawa 6 Feb 2019, 16:27
      #2

      Qt doesn't know the implementation of your model. For this to work you need to implement the virtual insertRows() method to actually insert the new item. In case of this simple model it would just do something like

      bool ReactiveModelTable::insertRows(int row, int count, const QModelIndex &parent)
      {
         beginInsertRows(parent, row, row + count - 1);
         rows += count;
         endInsertRows();
         return true;
      }
      
      1 Reply Last reply
      2
      • J Offline
        J Offline
        jss193
        wrote on 2 Jun 2019, 20:33 last edited by
        #3

        @Chris-Kawa said in Adding rows to QAbstractTableModel:

        insertRows(int row, int count, const QModelIndex &parent)

        But I can't initialize it on my clicked button, I did the following code:

        void cmyclass::on_add_button_clicked()
        {
            int a = modelo->rowCount(QModelIndex());
            modelo->insertRows(a,1,QModelIndex());
        
        }   
        

        It returns:
        error: non-const lvalue reference to type 'QModelIndex' cannot bind to a temporary of type 'QModelIndex'
        modelo->insertRows(a,1,QModelIndex());
        ^~~~~~~~~~~~~

        C 1 Reply Last reply 2 Jun 2019, 20:41
        0
        • J jss193
          2 Jun 2019, 20:33

          @Chris-Kawa said in Adding rows to QAbstractTableModel:

          insertRows(int row, int count, const QModelIndex &parent)

          But I can't initialize it on my clicked button, I did the following code:

          void cmyclass::on_add_button_clicked()
          {
              int a = modelo->rowCount(QModelIndex());
              modelo->insertRows(a,1,QModelIndex());
          
          }   
          

          It returns:
          error: non-const lvalue reference to type 'QModelIndex' cannot bind to a temporary of type 'QModelIndex'
          modelo->insertRows(a,1,QModelIndex());
          ^~~~~~~~~~~~~

          C Offline
          C Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on 2 Jun 2019, 20:41 last edited by
          #4

          @jss193 It means you forgot the const qualifier in the insertRows method. Make sure it looks exactly like this

          bool ReactiveModelTable::insertRows(int row, int count, const QModelIndex &parent) override;
          

          Use the override keyword when you override a virtual method. It will help you catch situations like this, and pay attention to consts. They are important.

          1 Reply Last reply
          2

          1/4

          2 Jun 2019, 16:01

          • Login

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