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. Confusion about inserting new data into QTableModel
QtWS25 Last Chance

Confusion about inserting new data into QTableModel

Scheduled Pinned Locked Moved Solved General and Desktop
qtableviewsetdataqtablemodel
4 Posts 3 Posters 2.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.
  • L Offline
    L Offline
    lansing
    wrote on 17 Jun 2020, 22:22 last edited by
    #1

    Hi i want to create a table to hold data that I pass in and I am confused on which function I should be using from the QTableModel when implementing.

    I have a list of set of values to be inserted into the table. The values are name, age, score. I start off creating a QTableView in the ui, then I subclassed the QTableModel as StudentModel. And then I created a vector in the model to hold the data.

    StudentModel.h

    struct studentInfo {
        QString name;
        int age;
        double score;
    } 
    
    QVector<studentInfo> m_studentReport
    
    

    Now the problem is that I don't know what to do next. QT Creator created a model template for me and there are several functions it needs me to reimplement.

    This is the thing I did so far:

    StudentModel.cpp

    int StudentModel::rowCount(const QModelIndex &parent) const
    {
        if (parent.isValid())
            return 0;
    
        return m_studentReport.count();
    }
    
    int StudentModel::columnCount(const QModelIndex &parent) const
    {
        if (parent.isValid())
            return 0;
    
        return 3;
    }
    
    QVariant StudentModel::data(const QModelIndex &index, int role) const
    {
        if (!index.isValid())
            return QVariant();
    
        int row = index.row();
        int col = index.column();
    
        if (role == Qt::DisplayRole && col == 0)
                return m_studentReport[row].name;
    
        if (role == Qt::DisplayRole && col == 1)
                return m_studentReport[row].age;
    
        if (role == Qt::DisplayRole && col == 2)
                return m_studentReport[row].score;
    
        return QVariant();
    }
    

    There are the setData and insertRows that looks to be the functions for inserting data. But insertRows is nothing more than adding a new empty row in the view and setData looks to be designed for single value insert only, where I need three at a time.

    And I found that I can simply add a new row by using the vector append function and it will show up in the table view.

    void StudentModel::addNewReport(QString a_name, int a_age, double a_score)
    {
        studentInfo data= {a_name, a_age, a_score};
        m_studentReport.append(data);
    }
    

    So what are the two functions for in my case and when do I need them?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 18 Jun 2020, 05:15 last edited by
      #2

      All you wrote is basically correct :-)

      setData is indeed for modifications of single cells. This is used for example when your table is editable and user changes your student's name.

      If you want to insert rows programmatically, or when loading your data from source, just append entries to your vector (don't forget to call beginInsertRows() and endInsertRows()).

      (Z(:^

      1 Reply Last reply
      1
      • V Offline
        V Offline
        VRonin
        wrote on 18 Jun 2020, 07:28 last edited by VRonin
        #3
        void StudentModel::addNewReport(QString a_name, int a_age, double a_score)
        {
        const int newRow = m_studentReport.size();
        beginInsertRows(QModelIndex(),newRow,newRow);
            m_studentReport.append(studentInfo{a_name, a_age, a_score});
        endInsertRows();
        }
        

        "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

        L 1 Reply Last reply 18 Jun 2020, 14:44
        3
        • V VRonin
          18 Jun 2020, 07:28
          void StudentModel::addNewReport(QString a_name, int a_age, double a_score)
          {
          const int newRow = m_studentReport.size();
          beginInsertRows(QModelIndex(),newRow,newRow);
              m_studentReport.append(studentInfo{a_name, a_age, a_score});
          endInsertRows();
          }
          
          L Offline
          L Offline
          lansing
          wrote on 18 Jun 2020, 14:44 last edited by
          #4

          @VRonin

          Thanks, a written example is a lot easier to understand. I was going through the Qt documentation over and over and couldn't find an good example for this.

          1 Reply Last reply
          0

          1/4

          17 Jun 2020, 22:22

          • 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