QSqlTableModel::setRecord(): emit dataChanged() consistently
authorMark Brand <mabrand@mabrand.nl>
Mon, 6 Feb 2012 15:05:53 +0000 (16:05 +0100)
committerQt by Nokia <qt-info@nokia.com>
Wed, 8 Feb 2012 13:43:18 +0000 (14:43 +0100)
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>
src/sql/models/qsqltablemodel.cpp
tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp

index 7210ddc..437712d 100644 (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;
index 518c6b6..a819cb7 100644 (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));
             }
         }