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?

    thanks

    @#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
    {
    Q_OBJECT

    public:
    explicit CsvReader(QWidget *parent = 0);
    ~CsvReader();

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

    private:
    Ui::CsvReader *ui;
    QList<QStringList> csv;
    QStandardItemModel model;
    QList<QStandardItem
    > 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;
      model->insertRows(row,1);
    
      // position within the model
      QModelIndex index = model->index(row,column);
    
      // selects current item you are looking for
      ui->tableView->setCurrentIndex(index);
    
      // im stuck here.
      // i wish to add 99 to the last row and 9th column of the model.
    
      //QStandardItem *item = new QStandardItem(dia.getvalues());
      //standardItemList.append(item);
      //model->appendRow(standardItemList);
      //standardItemList.clear();
    
    }
    

    }@


  • Lifetime Qt Champion

    Hi,

    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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.