-
Bug
-
Resolution: Fixed
-
P2: Important
-
5.8.0
-
None
-
Windows 10, x64
-
c0aabbd8a (dev), 4ab6fc721 (6.8), 6feeb7250 (6.7), a3ef7eed3 (tqtc/lts-6.5)
If multiple relations are set on a QSqlRelationalTableModel, the relations can crash when updating the relation model values.
QSqlRelationalTableModel model; model.setTable("table"); model.setRelation(0, QSqlRelation("table2", "id", "Name")); model.setRelation(1, QSqlRelation("table3", "id", "Name")); // Use model // Foreign keys changed, update models QSqlTableModel* relationModel = model.relationModel(0); if(relationModel) { relationModel->select(); // Crash here }
Looking at the internal code:
https://github.com/qt/qtbase/blob/dev/src/sql/models/qsqlrelationaltablemodel.cpp
QSqlRelationalTableModel::setRelation()
Resizes the d->relations vector.
However, each QRelatedTableModel stores an internal pointer into this array. This pointer becomes invalid when a new relationship is added and the array is resized.
A potential fix is to either update all existing pointers on array resize, or store a parent class pointer and index in QRelatedTableModel instead of a pointer to the array element.
A workaround is to set a dummy relation on a column that exceeds the data column size on creation.
QSqlRelationalTableModel::setRelation(ModelColumnCount + 1, QSqlRelation())
This pre-sizes the internal array, so a later resize does not need to occur.