From 698e620aad68d702c32b62e8114fe12a7b3e2ca5 Mon Sep 17 00:00:00 2001 From: Mark Brand Date: Tue, 14 Feb 2012 23:45:44 +0100 Subject: [PATCH] QSqlTableModel: derive primary values from database values Primary values are used to map a row in the model to a row in the database table. It is critically important between submitting a change and the following select (which refreshes the query) to have updated primary values. Otherwise, if the change affected the primary values, additional changes before select will misbehave. Change-Id: I5d08dd70ac5d3f06cd9d3186a439f4c80a037c2d Reviewed-by: Yunqiao Yin --- src/sql/models/qsqltablemodel.cpp | 11 +---------- src/sql/models/qsqltablemodel_p.h | 12 ++++++++++++ 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/sql/models/qsqltablemodel.cpp b/src/sql/models/qsqltablemodel.cpp index d31e4bc..a2999b3 100644 --- a/src/sql/models/qsqltablemodel.cpp +++ b/src/sql/models/qsqltablemodel.cpp @@ -201,16 +201,7 @@ bool QSqlTableModelPrivate::exec(const QString &stmt, bool prepStatement, QSqlRecord QSqlTableModelPrivate::primaryValues(int row) const { - Q_Q(const QSqlTableModel); - if (cache.value(row).op() == Insert) - return QSqlRecord(); - - QSqlRecord values(primaryIndex.isEmpty() ? rec : primaryIndex); - - for (int i = 0; i < values.count(); ++i) - values.setValue(i, q->QSqlQueryModel::data(createIndex(row, rec.indexOf(values.fieldName(i))), Qt::EditRole)); - - return values; + return cache.value(row).primaryValues(primaryIndex.isEmpty() ? rec : primaryIndex); } /*! diff --git a/src/sql/models/qsqltablemodel_p.h b/src/sql/models/qsqltablemodel_p.h index 7266810..57051a0 100644 --- a/src/sql/models/qsqltablemodel_p.h +++ b/src/sql/models/qsqltablemodel_p.h @@ -113,6 +113,18 @@ public: } inline bool submitted() const { return m_submitted; } inline void setSubmitted() { m_submitted = true; } + inline QSqlRecord primaryValues(const QSqlRecord& pi) const + { + if (m_op == None || m_op == Insert) + return QSqlRecord(); + + QSqlRecord values(pi); + + for (int i = values.count() - 1; i >= 0; --i) + values.setValue(i, m_db_values.value(values.fieldName(i))); + + return values; + } private: void init_rec() { -- 2.7.4