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>
This commit is contained in:
parent
04478614c1
commit
a694b9f8d2
@ -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
|
||||
|
@ -72,6 +72,7 @@ public:
|
||||
|
||||
void prefetch(int);
|
||||
void initColOffsets(int size);
|
||||
int columnInQuery(int modelColumn) const;
|
||||
|
||||
mutable QSqlQuery query;
|
||||
mutable QSqlError error;
|
||||
|
@ -1433,6 +1433,14 @@ void tst_QSqlTableModel::emptyTable()
|
||||
QVERIFY_SQL(model, select());
|
||||
QCOMPARE(model.rowCount(), 0);
|
||||
QCOMPARE(model.columnCount(), 1);
|
||||
|
||||
// QTBUG-29108: check correct horizontal header for empty query with pending insert
|
||||
QCOMPARE(model.headerData(0, Qt::Horizontal).toString(), QString("id"));
|
||||
model.setEditStrategy(QSqlTableModel::OnManualSubmit);
|
||||
model.insertRow(0);
|
||||
QCOMPARE(model.rowCount(), 1);
|
||||
QCOMPARE(model.headerData(0, Qt::Horizontal).toString(), QString("id"));
|
||||
model.revertAll();
|
||||
}
|
||||
|
||||
void tst_QSqlTableModel::tablesAndSchemas()
|
||||
|
Loading…
Reference in New Issue
Block a user