Hi,
I have a relationalTableModel with one relation
cxcModel->setTable("crop_x_cultivar");
cxcModel->setHeaderData(1, Qt::Horizontal, tr("Frucht"));
cxcModel->setHeaderData(2, Qt::Horizontal, tr("Sorte"));
cxcModel
->setRelation
(1,
QSqlRelation("crop",
"crop_id",
"crop_name"));
cxcModel->select();
ui.tableViewCxC->setModel(cxcModel);
ui.tableViewCxC->hideColumn(0);
cxcModel = new QSqlRelationalTableModel;
cxcModel->setTable("crop_x_cultivar");
cxcModel->setHeaderData(1, Qt::Horizontal, tr("Frucht"));
cxcModel->setHeaderData(2, Qt::Horizontal, tr("Sorte"));
cxcModel->setRelation(1, QSqlRelation("crop", "crop_id", "crop_name"));
cxcModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
cxcModel->select();
ui.tableViewCxC->setModel(cxcModel);
ui.tableViewCxC->hideColumn(0);
ui.tableViewCxC->setItemDelegate(new QSqlRelationalDelegate(ui.tableViewCxC));
To copy to clipboard, switch view to plain text mode
I add data with a form (see sreenshots) and the user can submit the data by pressing a save button. When pressing the "add" button i only insert a record in the cxcModel. When the "add" button is pressed the view shows the crop_id but not the crop_name. When i activate the cell or press "save" the right name (via the foreign key) is shown.
From the hidden column of the comboBox i get the crop_id
void BasicDataDialog::on_toolButtonAddCultivar_clicked()
{
if(!ui.lineEditCultivar->text().isEmpty()){
idx
= cropModel
->index
(ui.
comboBoxCrop->currentIndex
(),
0,
QModelIndex());
rec.setNull(0);
rec.setValue(1, cropModel->data(idx).toInt());
rec.setValue(2, ui.lineEditCultivar->text());
if(cxcModel->insertRecord(-1, rec));
ui.pushButtonSave->show();
}
}
void BasicDataDialog::on_toolButtonAddCultivar_clicked()
{
if(!ui.lineEditCultivar->text().isEmpty()){
QSqlRecord rec = cxcModel->record();
QModelIndex idx;
idx = cropModel->index(ui.comboBoxCrop->currentIndex(), 0, QModelIndex());
rec.setNull(0);
rec.setValue(1, cropModel->data(idx).toInt());
rec.setValue(2, ui.lineEditCultivar->text());
if(cxcModel->insertRecord(-1, rec));
ui.pushButtonSave->show();
}
}
To copy to clipboard, switch view to plain text mode
Any idea how to force the delegate to show the name (instead of the id) while the data is still cached? thx
Bookmarks