From a694b9f8d204d6555caf4e30dbd18f536859c5bd Mon Sep 17 00:00:00 2001 From: Mark Brand Date: Sat, 26 Jan 2013 23:09:24 +0100 Subject: fix QSqlTableModel::headerData() for empty query with inserted row QSqlQueryModel::headerData() relied on virtual indexInQuery() to detect whether the requested column at row 0 mapped to an index in the query. This failed when row 0 was a pending insert managed by QSqlTableModel, and therefore not in the query. The only thing that matters here is the column. Task-number: QTBUG-29108 Change-Id: I3e0ae85ba223e444781ec8033386d394bb44f0e8 Reviewed-by: Andy Shaw Reviewed-by: Mark Brand --- src/sql/models/qsqlquerymodel.cpp | 21 +++++++++++---------- src/sql/models/qsqlquerymodel_p.h | 1 + 2 files changed, 12 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/sql/models/qsqlquerymodel.cpp b/src/sql/models/qsqlquerymodel.cpp index 59103b72d3..00fc410ca5 100644 --- a/src/sql/models/qsqlquerymodel.cpp +++ b/src/sql/models/qsqlquerymodel.cpp @@ -95,6 +95,13 @@ void QSqlQueryModelPrivate::initColOffsets(int size) memset(colOffsets.data(), 0, colOffsets.size() * sizeof(int)); } +int QSqlQueryModelPrivate::columnInQuery(int modelColumn) const +{ + if (modelColumn < 0 || modelColumn >= rec.count() || !rec.isGenerated(modelColumn) || modelColumn >= colOffsets.size()) + return -1; + return modelColumn - colOffsets[modelColumn]; +} + /*! \class QSqlQueryModel \brief The QSqlQueryModel class provides a read-only data model for SQL @@ -370,11 +377,7 @@ QVariant QSqlQueryModel::headerData(int section, Qt::Orientation orientation, in val = d->headers.value(section).value(Qt::EditRole); if (val.isValid()) return val; - - // See if it's an inserted column (iiq.column() != -1) - QModelIndex dItem = indexInQuery(createIndex(0, section)); - - if (role == Qt::DisplayRole && d->rec.count() > section && dItem.column() != -1) + if (role == Qt::DisplayRole && d->rec.count() > section && d->columnInQuery(section) != -1) return d->rec.fieldName(section); } return QAbstractItemModel::headerData(section, orientation, role); @@ -668,12 +671,10 @@ bool QSqlQueryModel::removeColumns(int column, int count, const QModelIndex &par QModelIndex QSqlQueryModel::indexInQuery(const QModelIndex &item) const { Q_D(const QSqlQueryModel); - if (item.column() < 0 || item.column() >= d->rec.count() - || !d->rec.isGenerated(item.column()) - || item.column() >= d->colOffsets.size()) + int modelColumn = d->columnInQuery(item.column()); + if (modelColumn < 0) return QModelIndex(); - return createIndex(item.row(), item.column() - d->colOffsets[item.column()], - item.internalPointer()); + return createIndex(item.row(), modelColumn, item.internalPointer()); } QT_END_NAMESPACE diff --git a/src/sql/models/qsqlquerymodel_p.h b/src/sql/models/qsqlquerymodel_p.h index ecf69003f4..a79b62cda1 100644 --- a/src/sql/models/qsqlquerymodel_p.h +++ b/src/sql/models/qsqlquerymodel_p.h @@ -72,6 +72,7 @@ public: void prefetch(int); void initColOffsets(int size); + int columnInQuery(int modelColumn) const; mutable QSqlQuery query; mutable QSqlError error; -- cgit v1.2.3