PDA

View Full Version : Empty qsqlrelationaltablemodel



Charlie42
17th July 2015, 13:45
When I put model->select(); after relations, the model is empty, when I put it back before relations, the table displays correctly, but when I edit any cell the whole row becomes empty and gets "!" on the left. No errors.


QSqlRelationalTableModel *model = new QSqlRelationalTableModel(this, db1);
model->setTable("syllabi");
model->setEditStrategy(QSqlTableModel::OnFieldChange);
model->select();
model->setRelation(3, QSqlRelation("activity_types", "activity_type_id", "activity_type_name"));
model->setRelation(0, QSqlRelation("teachers", "teacher_id", "teacher_name"));
//model->select();
qDebug() << model->lastError();

ui->tableView->setModel(model);
ui->tableView->setItemDelegate(new QSqlRelationalDelegate(ui->tableView));
ui->tableView->horizontalHeader()->setStretchLastSection(true);
ui->tableView->setColumnHidden(9, true);

ChrisW67
18th July 2015, 05:19
If adding the related tables causes the rows to disappear it is probably because the value in the main table column is not present in the related table. Impossible to say without seeing the data.

If editing the row causes it to disappear then possibly the main table does not have a primary key (See http://doc.qt.io/qt-4.8/qsqlrelationaltablemodel.html#details).