QSqlTableModel::setRecord(): emit dataChanged() consistently

Previously, if any fields in the supplied record could not be matched
with a column in the target table, dataChanged() was supressed for all
columns for OnManualSubmit. This is not good because it prevents other
views from noticing the fields that *do* change.

It's simplest and probably more efficient just to emit
dataChanged() once for the whole row. Fewer signals need to be
processed and in typical cases much or all of the row is likely to
be changed anyway.

Change-Id: Ib56bf9a18e51b9cb85771acefcb2bf26e295a54e
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@nokia.com>
This commit is contained in:
Mark Brand 2012-02-06 16:05:53 +01:00 committed by Qt by Nokia
parent 5953304bc5
commit 7b726900ec
2 changed files with 8 additions and 13 deletions

View File

@ -1212,21 +1212,16 @@ bool QSqlTableModel::setRecord(int row, const QSqlRecord &record)
bool isOk = true;
for (int i = 0; i < record.count(); ++i) {
int idx = d->nameToIndex(record.fieldName(i));
if (idx == -1) {
if (idx == -1)
isOk = false;
} else if (d->strategy != OnManualSubmit) {
// historical bug: this could all be simple like OnManualSubmit, but isn't
const QModelIndex cIndex = createIndex(row, idx);
else
mrow.setValue(idx, record.value(i));
emit dataChanged(cIndex, cIndex);
} else {
mrow.setValue(idx, record.value(i));
}
}
if (d->strategy == OnManualSubmit && isOk)
if (columnCount())
emit dataChanged(createIndex(row, 0), createIndex(row, columnCount() - 1));
else if (d->strategy == OnFieldChange)
if (d->strategy == OnFieldChange)
return submit();
else if (d->strategy == OnManualSubmit)
return isOk;

View File

@ -412,10 +412,10 @@ void tst_QSqlTableModel::setRecord()
model.submit();
else {
// dataChanged() is not emitted when submitAll() is called
QCOMPARE(spy.count(), model.columnCount());
QCOMPARE(spy.count(), 1);
QCOMPARE(spy.at(0).count(), 2);
QCOMPARE(qvariant_cast<QModelIndex>(spy.at(1).at(0)), model.index(i, 1));
QCOMPARE(qvariant_cast<QModelIndex>(spy.at(1).at(1)), model.index(i, 1));
QCOMPARE(qvariant_cast<QModelIndex>(spy.at(0).at(0)), model.index(i, 0));
QCOMPARE(qvariant_cast<QModelIndex>(spy.at(0).at(1)), model.index(i, rec.count() - 1));
}
}