Reputation: 14106
QTableView *tableView = new QTableView();
QStandardItemModel *model = new QStandardItemModel(15,4, this);
tableView->setModel(model);
model->setHorizontalHeaderItem(0, new QStandardItem("#") );
model->setHorizontalHeaderItem(1, new QStandardItem("col1") );
model->setHorizontalHeaderItem(2, new QStandardItem("col2") );
// update tableView
model->removeRows(0,model->rowCount());
QList<QStandardItem*> row1;
row1 << new QStandardItem("element 01") << new QStandardItem("element 02") << new QStandardItem("element 03");
model->appendRow( row1 );
QList<QStandardItem*> row2;
row2 << new QStandardItem("element 04") << new QStandardItem("element 05") << new QStandardItem("element 06");
model->appendRow( row2 );
The tableView
will be frequently updated (all the content will be deleted and replaced).
what is the fastest way (less lines of code) to update this QTableView
, maybe using some structure of data, and a QStandardItemModel
method that handles directly this structure ?
something like:
model->setNewData( theNewData );
Upvotes: 3
Views: 3794
Reputation: 32665
There is no need to remove rows and add new ones. You can just update the values for the current items like:
model->item(0, 0)->setData("element 01",Qt::DisplayRole);
model->item(0, 1)->setData("element 02",Qt::DisplayRole);
model->item(0, 2)->setData("element 03",Qt::DisplayRole);
model->item(1, 0)->setData("element 04",Qt::DisplayRole);
model->item(1, 1)->setData("element 05",Qt::DisplayRole);
model->item(1, 2)->setData("element 06",Qt::DisplayRole);
Upvotes: 1