QStandardItemModel does not update QTableView after new Row insert



  • 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?



  • 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!



  • @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?



  • 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.


Log in to reply
 

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