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. QStandardItemModel does not update QTableView after new Row insert
Forum Updated to NodeBB v4.3 + New Features

QStandardItemModel does not update QTableView after new Row insert

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 1.7k 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.
  • P Offline
    P Offline
    Panoss
    wrote on last edited by Panoss
    #1

    I insert a new row in a QStandardItemModel like this:

    // insert row in the model        
    const int newRow = model->rowCount();
    model->setData(model->index(newRow, 3), name);
    model->setData(model->index(newRow, 4), price);
    

    But the QTableView does not display the new record.

    But, if I 'refill' the query (this code is in a function which I call to 'fill' the model):

        QSqlQuery query;
        query.prepare(SQL);
        if (query.exec()) {
            for (bool firstRun = true; query.next();) {
                const QSqlRecord currRecord = query.record();
                if (firstRun) {
                    firstRun = false;
                    model->insertColumns(0, currRecord.count());
                    for (int i = 0; i < currRecord.count(); ++i)                
                        model->setHeaderData(i, Qt::Horizontal, currRecord.fieldName(i));                  
                }
                const int newRow = model->rowCount();
                model->insertRow(newRow);
                for (int i = 0; i < currRecord.count(); ++i)
                    model->setData(model->index(newRow, i), currRecord.value(i));
            }           
        }
    

    ...the table updates and displays the new row, obviously because the model gets updated.

    Isn't there something that updates a QStandardItemModel after ner row insert?

    1 Reply Last reply
    0
    • P Offline
      P Offline
      Panoss
      wrote on last edited by
      #2

      Ok, I resolved it like this:

      QList<QStandardItem*> newRow;
      
      QStandardItem* itm = new QStandardItem(QString(name)); newRow.append(itm);
      itm = new QStandardItem(QString(price)); newRow.append(itm);
      
      model->appendRow(newRow);
      

      This way new row is displayed immediately!

      VRoninV 1 Reply Last reply
      0
      • P Panoss

        Ok, I resolved it like this:

        QList<QStandardItem*> newRow;
        
        QStandardItem* itm = new QStandardItem(QString(name)); newRow.append(itm);
        itm = new QStandardItem(QString(price)); newRow.append(itm);
        
        model->appendRow(newRow);
        

        This way new row is displayed immediately!

        VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        @Panoss said in QStandardItemModel does not update QTableView after new Row insert:

        Ok, I resolved it like this:

        No, that is radically not the same thing.

        But the QTableView does not display the new record.

        What do you mean? in the loop of (i.e. you cannot see the rows added one by one) or in the final result?

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        0
        • P Offline
          P Offline
          Panoss
          wrote on last edited by Panoss
          #4

          I add one row only, by clicking a QPushButton.
          I used to add a row and nothing would be displayed (before I change to appendRow).
          You mean I should not use the appendRow? Why? It seems to work fine.

          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