Unsolved QTableView Not Updating After Adding a New Row
-
Thank You so much for your time brother.
But sadly it didn't work. Actually, I want the view to update automatically after adding the new row through a dialog box. As I can see the changes if I reopen the particular window again.
Also, my model has the updated data but somehow I don't know why table view is not updating.
Thanks
-
Hmmm...try
emit dataChanged(model->index(i, 0), model->index(i, 1), <maybe you'll have to pass 3rd parameter as well>)
just after the line which adds new row.
If that won't help then there are last resort methods -beginResetModel()
before adding rows, and after completing add row operationendResetModel()
Soo, merging my answers so far the output code would be:for (int i = 0; i < clusterNames.length(); i++) { model->appendRow( { new QStandardItem(clusterNames[i]), new QStandardItem(clusterIPs[i]) } ); emit dataChanged(model->index(i, 0), model->index(i, 1));//maybe you'll have to pass 3rd parameter as well }
-
@Satish-Joshi said in QTableView Not Updating After Adding a New Row:
Actually, I want the view to update automatically after adding the new row through a dialog box.
i explained why it doesnt update.
What exactly have you tried?! -
Yes I understand what you trying to explain and have used your suggested approach and
I have used QStandardItemModel::insertRow(). But it didn't work.Thanks
-
you mean to say QAbstractItemModel::dataChanged(.....) right?
I did it like this way :
// In header File signals: void dataChanged(const QModelIndex & topLeft, const QModelIndex & bottomRight); // On the Loop emit model->dataChanged(model->index(i, 0), model->index(i, 1));
Sorry but this time it also didn't work or I am missing something. Please let me know
Thanks
-
@Satish-Joshi
you are not doing what i suggested.
Try this:model = new QStandardItemModel(this); model->setColumnCount( 2 ); //... for(int i = 0; i < clusterNames.length(); i++) { QStandardItem *clusterName_i = new QStandardItem(clusterNames[i]); QStandardItem *clusterIP_i = new QStandardItem(clusterIPs[i]); model->appendRow( QList<QStandardItem*>() << clusterName_i << clusterIP_i ); }
-
Sorry. It didn't work though
I think there might be some issue with Widget or QSubWindow because as I already mentioned that the values are already updating on the model (checked through debugging it) but couldn't be displayed on the View.
As if I reopen the window it displays properly.
What do you think?
Thanks Anyway
-
Use that
beginResetModel()
andendResetModel()
methods then, these makes a model completely invalidated, and therefore force a thorough update.model->beginResetModel(); for (int i = 0; i < clusterNames.length(); i++) { model->appendRow( { new QStandardItem(clusterNames[i]), new QStandardItem(clusterIPs[i]) } ); } model->endResetModel();
-
-
Inherit QStandardItemModel, and declare public functions like
beginReset()
andendReset()
whose internally invoke these protected methods.