Hi !
I created a class for editing database tables (SQLITE) with the relationship ( LEFT JOIN ) to another table with enable NULL for FK.
Example:
master table:
driver:
ID, NAME, CAR
subtable:
cars:
ID, TYPE, COLOR
Here is a snippet of code:
...
...
rtblmdl->setTable("driver");
...
// join to QTableView ( tblW )
ui->tblW->setModel(mrtblmdl);
ui->tblW->hideColumn(0);
...
...
// set record "QSqlRecord" ( rec is private variable ) after editting
rec.setValue(i,lnEdt[i]->text());
...
// write in to table
rtblmdl->setRecord(ui->tblW->currentIndex().row(),rec);
rtblmdl->submitAll();
...
// write into table another method
rtblmdl
->setData
(rtblmdl
->index
(ui
->tblW
->currentIndex
().
row(),
5),
QVariant::fromValue(1));
rtblmdl->submitAll();
...
rtblmdl = new QSqlRelationalTableModel(this,ggdb);
...
rtblmdl->setTable("driver");
rtblmdl->setRelation(2,QSqlRelation("cars","id","car"));
rtblmdl->setJoinMode(QSqlRelationalTableModel::LeftJoin);
rtblmdl->setEditStrategy(QSqlTableModel::OnManualSubmit);
...
// join to QTableView ( tblW )
ui->tblW->setModel(mrtblmdl);
ui->tblW->setItemDelegate(new QSqlRelationalDelegate(ui->tblW));
ui->tblW->hideColumn(0);
...
...
// set record "QSqlRecord" ( rec is private variable ) after editting
rec.setValue(i,lnEdt[i]->text());
...
// write in to table
rtblmdl->setRecord(ui->tblW->currentIndex().row(),rec);
rtblmdl->submitAll();
...
// write into table another method
rtblmdl->setData(rtblmdl->index(ui->tblW->currentIndex().row(),5),QVariant::fromValue(1));
rtblmdl->submitAll();
To copy to clipboard, switch view to plain text mode
The cells of the master table ( "driver" ) I do not editing by qtableview, but separate EditBoxs (one EditBox for each column ).
However, if I need erase cell with FK (cancel a relationship), i setting the appropriate item in QSqlRecord to NULL, this value after call methods QSqlRelationalTableModel.SubmitAll not write ( not delete relationship )
I also tried writing by QSqlRelationalTableModel.setData unfortunately with the same result.
Thanks fro any response.
Bookmarks