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 <axel.spoerl@qt.io>
This commit is contained in:
parent
8f8ce8d7a7
commit
608da52774
@ -428,8 +428,9 @@ QVariant QSqlRelationalTableModel::data(const QModelIndex &index, int role) cons
|
|||||||
example, if \a index is out of bounds).
|
example, if \a index is out of bounds).
|
||||||
|
|
||||||
For relational columns, \a value must be the index, not the
|
For relational columns, \a value must be the index, not the
|
||||||
display value. The index must also exist in the referenced
|
display value. If an index is given, it must also exist in the
|
||||||
table, otherwise the function returns \c false.
|
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()
|
\sa editStrategy(), data(), submit(), revertRow()
|
||||||
*/
|
*/
|
||||||
@ -442,7 +443,7 @@ bool QSqlRelationalTableModel::setData(const QModelIndex &index, const QVariant
|
|||||||
auto relation = d->relations.at(index.column());
|
auto relation = d->relations.at(index.column());
|
||||||
if (!relation->isDictionaryInitialized())
|
if (!relation->isDictionaryInitialized())
|
||||||
relation->populateDictionary();
|
relation->populateDictionary();
|
||||||
if (!relation->dictionary.contains(value.toString()))
|
if (value.isValid() && !relation->dictionary.contains(value.toString()))
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return QSqlTableModel::setData(index, value, role);
|
return QSqlTableModel::setData(index, value, role);
|
||||||
|
@ -402,6 +402,26 @@ void tst_QSqlRelationalTableModel::setData()
|
|||||||
QCOMPARE(model.data(model.index(0,1)).toString(), QString("Hr"));
|
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()
|
void tst_QSqlRelationalTableModel::multipleRelation()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user