QSqlTableModel::setRecord(): do not try to detect value changes
authorMark Brand <mabrand@mabrand.nl>
Mon, 6 Feb 2012 14:03:25 +0000 (15:03 +0100)
committerQt by Nokia <qt-info@nokia.com>
Wed, 8 Feb 2012 13:43:18 +0000 (14:43 +0100)
In an apparent attempt to be economical with emitting dataChanged()
and submitting SQL to the databse, setRecord() compares each field
value of the record with the old value, taking action only when
a difference is detected.  Several complaints against this code are:

-The comparision does not work on float type.

-It is really up to the application and database to decide this. The
model should make few assumptions. The application has the option to
omit fields from the record that should be ignored.

-The current behavior seems to assume that the "old" values are the
current state of the database, but the database may have changed since
the model was last refreshed.

-The code compares the value from record(), which probably
corresponds to the EditRole, with the DisplayRole value from data().

Change-Id: I11477c185eb411d442144dc682893d0df12d03d5
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@nokia.com>
src/sql/models/qsqltablemodel.cpp
tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp

index 1746883..7210ddc 100644 (file)
@@ -1217,15 +1217,8 @@ bool QSqlTableModel::setRecord(int row, const QSqlRecord &record)
         } else if (d->strategy != OnManualSubmit) {
             // historical bug: this could all be simple like OnManualSubmit, but isn't
             const QModelIndex cIndex = createIndex(row, idx);
-            // historical bug: comparing EditRole with DisplayRole values here
-            const QVariant oldValue = data(cIndex);
-            const QVariant value = record.value(i);
-            // historical bug: it's a bad idea to check for change here
-            // historical bug: should test oldValue.isNull() != value.isNull()
-            if (oldValue.isNull() || oldValue != value) {
-                mrow.setValue(idx, record.value(i));
-                emit dataChanged(cIndex, cIndex);
-            }
+            mrow.setValue(idx, record.value(i));
+            emit dataChanged(cIndex, cIndex);
         } else {
             mrow.setValue(idx, record.value(i));
         }
index 6a827e6..518c6b6 100644 (file)
@@ -412,10 +412,10 @@ void tst_QSqlTableModel::setRecord()
                 model.submit();
             else {
                 // dataChanged() is not emitted when submitAll() is called
-                QCOMPARE(spy.count(), 2);
+                QCOMPARE(spy.count(), model.columnCount());
                 QCOMPARE(spy.at(0).count(), 2);
-                QCOMPARE(qvariant_cast<QModelIndex>(spy.at(0).at(0)), model.index(i, 1));
-                QCOMPARE(qvariant_cast<QModelIndex>(spy.at(0).at(1)), model.index(i, 1));
+                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));
             }
         }