Refreshing one or more rows in a table view
I have a QSqlQueryModel derived class and a QTableView derived class making use of Qt's model-view architecture to display data.
When the user double clicks on a row on the view, a dialog comes up and lets the user save a record. When the dialog is dismissed, I need to refresh the table view for just one row. I tried this by having a method in my derived model object:
void RefreshRows(const QModelIndex & topLeft, const QModelIndex & bottomRight)
{
dataChanged(topLeft, bottomRight);
}
I call this method after the dialog is dismissed:
pMyDerivedModel->RefreshRows(topLeft, bottomRight);
But the view's contents are not changed. Is this the right way to refresh one or more rows?
Thank you.
Re: Refreshing one or more rows in a table view
The view's contents is not changed because the model content didn't change. How did you reimplement setData() for the model?
Re: Refreshing one or more rows in a table view
If the data would change the model will emit appropriate signal (dataChanged()). And if it's a signal, you have to emit it:
Code:
emit dataChanged(something1, something2);
but you should not need to do that.
Re: Refreshing one or more rows in a table view
Unfortunately, I have no idea how to implement setData in this case but I have tried something like this:
Code:
{
if (index.isValid() && role == Qt::DisplayRole)
{
QMap<int, QVariant> itemData;
itemData.insert(Qt::DisplayRole, value);
setItemData(index, itemData);
emit(dataChanged(index, index));
return true;
}
return false;
}
But this does not work. How do I get hold of a row on the model?
Thanks for your help. I was not in town, so I could not respond to your replies sooner.
Re: Refreshing one or more rows in a table view
The query model is read only. Such simple things won't work, especially that setItemData() does nothing for this model. There is an example bundled with Qt that shows how to make a query model writable although I doubt it will be of any use to you. Better switch to QSqlTableModel if you can.