Important: Please read the Qt Code of Conduct -

Editing existing model

  • I have a button that opens a dialog box.
    I populate it with data and pass the data to my main window.
    the code below manages to insert a new row to model.

    I can modify model from the view table.
    but I would like to insert a new row to model with the populated data from the dialog box.
    how do I do this?


    @#ifndef CSVREADER_H
    #define CSVREADER_H

    #include <QMainWindow>
    #include <QFile>
    #include <QFileDialog>
    #include <QTextStream>
    #include <QDebug>
    #include <QString>
    #include <QStandardItemModel>

    namespace Ui {
    class CsvReader;

    class CsvReader : public QMainWindow

    explicit CsvReader(QWidget *parent = 0);

    private slots:
    void on_action_Open_triggered();
    void checkString(QString &temp, QChar character = 0);

    void on_pushButton_clicked();
    void on_pushButton_3_clicked();
    void on_pushButton_2_clicked();

    Ui::CsvReader *ui;
    QList<QStringList> csv;
    QStandardItemModel model;
    > standardItemList;


    #endif // CSVREADER_H

    void CsvReader::on_pushButton_clicked()
    // add

    // get row count
    Dialog dia(this);
    if (dia.exec&#40;&#41;)
      // returns a value to be inserted to a cell
     // dia.getvalues returns 99
      qDebug() << dia.getvalues();
      int row = model->rowCount();
      int column = model->columnCount();
      // position how mnay you want to insert;
      // position within the model
      QModelIndex index = model->index(row,column);
      // selects current item you are looking for
      // im stuck here.
      // i wish to add 99 to the last row and 9th column of the model.
      //QStandardItem *item = new QStandardItem(dia.getvalues());


  • Lifetime Qt Champion


    Something like that should be enough:

    QStandardItem *item = new QStandardItem(dia.getvalues());
    model->setItem(row, item);

    Why are you keeping list of QStandardItem * since you already have a model ?

Log in to reply