View Full Version : Refreshing one or more rows in a table view

William Wilson
18th May 2009, 02:12
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.

18th May 2009, 05:37
The view's contents is not changed because the model content didn't change. How did you reimplement setData() for the model?

18th May 2009, 09:05
If the data would change the model will emit appropriate signal (dataChanged()). And if it's a signal, you have to emit it:

emit dataChanged(something1, something2);
but you should not need to do that.

William Wilson
24th May 2009, 23:28
Unfortunately, I have no idea how to implement setData in this case but I have tried something like this:

bool CMyQueryModel::setData(const QModelIndex &index, const QVariant &value, int role)
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.

25th May 2009, 01:10
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.