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. updating rows in QStandardItemModel
QtWS25 Last Chance

updating rows in QStandardItemModel

Scheduled Pinned Locked Moved Solved General and Desktop
qt 5listview
5 Posts 3 Posters 6.3k 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.
  • S Offline
    S Offline
    Shawny
    wrote on 29 Mar 2016, 22:11 last edited by Shawny
    #1

    Using the documentation provided here and some research, I was able to implement a program which updates listView whenever user sends some text from text box.

    Below given function works well but I only have one small problem of updating row as user clicks. listView overwrites the previous entry as the user sends new text from text box by clicking button. It has something to do with row value getting incremented dynamically.

    void MainWindow::on_pushButton_clicked()
    {
    
        QStandardItemModel* model = new QStandardItemModel(this);
        QStandardItem* rowlist = new QStandardItem(QString(ui->textEdit->toPlainText()));
        int row=0; // what should be value here
        model -> setItem(row++, 0, rowlist); //how to update new row dynamically on every new button press
        ui->listView->setModel(model);
    
    }
    

    I want values of listView printed on individual row as user presses button. In above code I believe I am missing just very small logic.

    Thank you.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 29 Mar 2016, 22:30 last edited by
      #2

      Hi,

      Why are you creating a new model each time ? Just update the existing one.

      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
      • S Offline
        S Offline
        Shawny
        wrote on 29 Mar 2016, 22:39 last edited by Shawny
        #3

        To update the existing model I tried keeping these two declarations outside the function:

            QStandardItemModel* model = new QStandardItemModel(this);
            QStandardItem* rowlist = new QStandardItem(QString(ui->textEdit->toPlainText()));
        

        but it shows error as model not declared in scope. Can you tell me where I can put them? so that they can be available through out all the functions.

        this is my mainwindow.cpp

        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        #include "QStandardItemModel"
        
        MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
        
        }
        
        MainWindow::~MainWindow()
        {
            delete ui;
        
        }
        
        void MainWindow::on_pushButton_clicked()
        {
            QStandardItemModel* model = new QStandardItemModel(this);
            QStandardItem* rowlist = new QStandardItem(QString(ui->textEdit->toPlainText()));
            //model->appendRow(rowlist);
            int row=0;
            model -> setItem(row++, 0, rowlist);
            ui->listView->setModel(model);
        
        }
        

        Thank you.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 29 Mar 2016, 22:54 last edited by
          #4

          hi
          You must put
          QStandardItemModel* model;
          in mainwindow.h

          class MainWindow : public QMainWindow
          {
          Q_OBJECT
          QStandardItemModel* model;
          ...

          that way its available to all member functions of mainwindow.

          BUT this is just the declaration, you still need to new it.

          This you can do in mainwindow constructor
          MainWindow::MainWindow(QWidget* parent) { ...
          ui->setupUi(this);
          model = new QStandardItemModel(this);
          }

          the
          QStandardItem* rowlist = new QStandardItem(QString(ui->textEdit->toPlainText()));
          You can still keep in the
          MainWindow::on_pushButton_clicked()
          Also if you add new rows, you might be able to use ( if its a list)
          http://doc.qt.io/qt-5/qstandarditemmodel.html#appendRow-1
          That way you dont need to keep a row counter.
          Else setItem is fine.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Shawny
            wrote on 29 Mar 2016, 23:09 last edited by
            #5

            Works perfectly. Thank you very much. I previously used appendRow for updating rows but it didn't work as I wanted so tried setItem. But the main source of problem was creation of new models at every button push. Thank you for pointing it @SGaist and @mrjj

            1 Reply Last reply
            0

            1/5

            29 Mar 2016, 22:11

            • Login

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