From 7b726900ecdd1d4d5e9b8f7c34f00dd27524b1c6 Mon Sep 17 00:00:00 2001 From: Mark Brand Date: Mon, 6 Feb 2012 16:05:53 +0100 Subject: [PATCH] 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 --- src/sql/models/qsqltablemodel.cpp | 15 +++++---------- .../auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp | 6 +++--- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/sql/models/qsqltablemodel.cpp b/src/sql/models/qsqltablemodel.cpp index 7210ddc..437712d 100644 --- a/src/sql/models/qsqltablemodel.cpp +++ b/src/sql/models/qsqltablemodel.cpp @@ -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; diff --git a/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp b/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp index 518c6b6..a819cb7 100644 --- a/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp +++ b/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp @@ -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(spy.at(1).at(0)), model.index(i, 1)); - QCOMPARE(qvariant_cast(spy.at(1).at(1)), model.index(i, 1)); + QCOMPARE(qvariant_cast(spy.at(0).at(0)), model.index(i, 0)); + QCOMPARE(qvariant_cast(spy.at(0).at(1)), model.index(i, rec.count() - 1)); } } -- 2.7.4