summaryrefslogtreecommitdiffstats
path: root/src/sql/models/qsqlquerymodel.cpp
diff options
context:
space:
mode:
authorMark Brand <mabrand@mabrand.nl>2013-01-26 23:09:24 +0100
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-02-06 07:57:13 +0100
commita694b9f8d204d6555caf4e30dbd18f536859c5bd (patch)
treec1c58300e4f97468fa9d34fa07b7df14e7507c0e /src/sql/models/qsqlquerymodel.cpp
parent04478614c1f21de8ff9abb7fe9dc64c672065695 (diff)
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 <andy.shaw@digia.com> Reviewed-by: Mark Brand <mabrand@mabrand.nl>
Diffstat (limited to 'src/sql/models/qsqlquerymodel.cpp')
-rw-r--r--src/sql/models/qsqlquerymodel.cpp21
1 files changed, 11 insertions, 10 deletions
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