summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGabriel de Dietrich <gabriel.dietrich-de@nokia.com>2009-10-26 15:53:47 +0100
committerGabriel de Dietrich <gabriel.dietrich-de@nokia.com>2009-10-26 16:15:55 +0100
commit2d750192e73244f5b4ad6b451f264728d42669be (patch)
tree53616ffcc129e0568b4d6585ae1c554b7eda7061
parent9c78eb972041a066e455bf04d63f3290afacb982 (diff)
Fixed crash when setting header data in QSqlQueryModel.
The crash (Q_ASSERT_X failure) happened when a proxy model was being attached to the QSqlQueryModel, and no query was set yet. The headerDataChanged() signal was being received by the proxy model who wouldn't check its "proxyfied" data bounds. The patch introduces a behaviour change. However, this change makes the usage of QSqlQueryModel::setHeaderData() to be more in accordance with the current documentation, and to behave in the same way as for QStandardItemModel, QTreeModel, and QTableModel. Task-number: QTBUG-4963 Reviewed-by: Olivier
-rw-r--r--src/sql/models/qsqlquerymodel.cpp2
-rw-r--r--tests/auto/qsqlquerymodel/tst_qsqlquerymodel.cpp18
2 files changed, 15 insertions, 5 deletions
diff --git a/src/sql/models/qsqlquerymodel.cpp b/src/sql/models/qsqlquerymodel.cpp
index a72ad8c049..1719239dff 100644
--- a/src/sql/models/qsqlquerymodel.cpp
+++ b/src/sql/models/qsqlquerymodel.cpp
@@ -417,7 +417,7 @@ bool QSqlQueryModel::setHeaderData(int section, Qt::Orientation orientation,
const QVariant &value, int role)
{
Q_D(QSqlQueryModel);
- if (orientation != Qt::Horizontal || section < 0)
+ if (orientation != Qt::Horizontal || section < 0 || columnCount() <= section)
return false;
if (d->headers.size() <= section)
diff --git a/tests/auto/qsqlquerymodel/tst_qsqlquerymodel.cpp b/tests/auto/qsqlquerymodel/tst_qsqlquerymodel.cpp
index 3131f35788..02b48fab35 100644
--- a/tests/auto/qsqlquerymodel/tst_qsqlquerymodel.cpp
+++ b/tests/auto/qsqlquerymodel/tst_qsqlquerymodel.cpp
@@ -96,6 +96,7 @@ private slots:
void task_180617();
void task_180617_data() { generic_data(); }
+ void task_QTBUG_4963_setHeaderDataWithProxyModel();
private:
void generic_data(const QString &engine=QString());
@@ -428,6 +429,8 @@ void tst_QSqlQueryModel::setHeaderData()
QVERIFY(!model.setHeaderData(5, Qt::Vertical, "foo"));
QVERIFY(model.headerData(5, Qt::Vertical).isValid());
+ model.setQuery(QSqlQuery("select * from " + qTableName("test"), db));
+
qRegisterMetaType<Qt::Orientation>("Qt::Orientation");
QSignalSpy spy(&model, SIGNAL(headerDataChanged(Qt::Orientation, int, int)));
QVERIFY(model.setHeaderData(2, Qt::Horizontal, "bar"));
@@ -437,10 +440,8 @@ void tst_QSqlQueryModel::setHeaderData()
QCOMPARE(spy.value(0).value(1).toInt(), 2);
QCOMPARE(spy.value(0).value(2).toInt(), 2);
- QVERIFY(model.setHeaderData(7, Qt::Horizontal, "foo", Qt::ToolTipRole));
- QVERIFY(model.headerData(7, Qt::Horizontal, Qt::ToolTipRole).isValid());
-
- model.setQuery(QSqlQuery("select * from " + qTableName("test"), db));
+ QVERIFY(!model.setHeaderData(7, Qt::Horizontal, "foo", Qt::ToolTipRole));
+ QVERIFY(!model.headerData(7, Qt::Horizontal, Qt::ToolTipRole).isValid());
bool isToUpper = db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2");
QCOMPARE(model.headerData(0, Qt::Horizontal).toString(), isToUpper ? QString("ID") : QString("id"));
@@ -603,5 +604,14 @@ void tst_QSqlQueryModel::task_180617()
QCOMPARE(view.rowAt(0), -1);
}
+void tst_QSqlQueryModel::task_QTBUG_4963_setHeaderDataWithProxyModel()
+{
+ QSqlQueryModel plainModel;
+ QSortFilterProxyModel proxyModel;
+ proxyModel.setSourceModel(&plainModel);
+ QVERIFY(!plainModel.setHeaderData(0, Qt::Horizontal, QObject::tr("ID")));
+ // And it should not crash.
+}
+
QTEST_MAIN(tst_QSqlQueryModel)
#include "tst_qsqlquerymodel.moc"