diff options
author | Tobias Koenig <[email protected]> | 2015-08-11 15:09:46 +0200 |
---|---|---|
committer | Christian Ehrlicher <[email protected]> | 2024-09-04 06:17:52 +0200 |
commit | 608da527743ff5845fb40a76e80dced31bff0cb0 (patch) | |
tree | 0d8dd51ec3050864cae41cdab0eae302155e95a0 | |
parent | 8f8ce8d7a7e029e62a4f9b5b209dcc37f61410cb (diff) |
Allow resetting foreign key in QSqlRelationalTableModel
Change the QSqlRelationalTableModel::setData() method to remove the
associated foreign key, if called on a relational column.
Task-number: QTBUG-47713
Change-Id: Ib3cf03f1da506d63f1a59349d64d5da45bde4137
Reviewed-by: Axel Spoerl <[email protected]>
-rw-r--r-- | src/sql/models/qsqlrelationaltablemodel.cpp | 7 | ||||
-rw-r--r-- | tests/auto/sql/models/qsqlrelationaltablemodel/tst_qsqlrelationaltablemodel.cpp | 20 |
2 files changed, 24 insertions, 3 deletions
diff --git a/src/sql/models/qsqlrelationaltablemodel.cpp b/src/sql/models/qsqlrelationaltablemodel.cpp index d892be070e7..29beefb94db 100644 --- a/src/sql/models/qsqlrelationaltablemodel.cpp +++ b/src/sql/models/qsqlrelationaltablemodel.cpp @@ -428,8 +428,9 @@ QVariant QSqlRelationalTableModel::data(const QModelIndex &index, int role) cons example, if \a index is out of bounds). For relational columns, \a value must be the index, not the - display value. The index must also exist in the referenced - table, otherwise the function returns \c false. + display value. If an index is given, it must also exist in the + referenced table, otherwise the function returns \c false. + If a QVariant() is passed instead of an index, the index is cleared. \sa editStrategy(), data(), submit(), revertRow() */ @@ -442,7 +443,7 @@ bool QSqlRelationalTableModel::setData(const QModelIndex &index, const QVariant auto relation = d->relations.at(index.column()); if (!relation->isDictionaryInitialized()) relation->populateDictionary(); - if (!relation->dictionary.contains(value.toString())) + if (value.isValid() && !relation->dictionary.contains(value.toString())) return false; } return QSqlTableModel::setData(index, value, role); diff --git a/tests/auto/sql/models/qsqlrelationaltablemodel/tst_qsqlrelationaltablemodel.cpp b/tests/auto/sql/models/qsqlrelationaltablemodel/tst_qsqlrelationaltablemodel.cpp index aaf55da6e18..3879c0f2ae4 100644 --- a/tests/auto/sql/models/qsqlrelationaltablemodel/tst_qsqlrelationaltablemodel.cpp +++ b/tests/auto/sql/models/qsqlrelationaltablemodel/tst_qsqlrelationaltablemodel.cpp @@ -402,6 +402,26 @@ void tst_QSqlRelationalTableModel::setData() QCOMPARE(model.data(model.index(0,1)).toString(), QString("Hr")); } + // verify that clearing a foreign key works + { + QSqlRelationalTableModel model(0, db); + + model.setTable(reltest1); + model.setRelation(2, QSqlRelation(reltest2, "id", "title")); + model.setSort(0, Qt::AscendingOrder); + QVERIFY_SQL(model, select()); + + QVERIFY(model.setData(model.index(0, 1), QString("harry2"))); + QVERIFY(model.setData(model.index(0, 2), 2)); + + QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry2")); + QCOMPARE(model.data(model.index(0, 2)).toString(), QString("mister")); + + QVERIFY(model.setData(model.index(0, 2), QVariant())); // clear foreign key + + QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry2")); + QCOMPARE(model.data(model.index(0, 2)).toString(), QString()); // check that foreign value is not visible + } } void tst_QSqlRelationalTableModel::multipleRelation() |