Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Confusion about inserting new data into QTableModel



  • 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?


  • Moderators

    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()).



  • 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();
    }
    


  • @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.