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. Editing existing model
Forum Updated to NodeBB v4.3 + New Features

Editing existing model

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.0k Views 1 Watching
  • 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.
  • R Offline
    R Offline
    rafae11
    wrote on last edited by
    #1

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

    }@

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

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