How to handle the OnRowChange EditStrategy in master/detail tables?
-
I have two
QSqlRelationalTableModel
, since each table has foreign keys to others.
These two tables are in a master/detail relation.
I set up both tables in this way:QSqlRelationalTableModel *_model = nullptr; QSqlDatabase db = QSqlDatabase::database(DATABASE_NAME); _model = new QSqlRelationalTableModel(this, db); _model->setTable(table_name); _model->setEditStrategy(QSqlRelationalTableModel::OnRowChange); tableView->setModel(_model); tableView->setItemDelegate(delegate); tableView->setFocusPolicy(Qt::StrongFocus);
Then I use this code to retrieve the associated rows when the user select a row in the master table:
connect(ui->tableMaster->selectionModel(), &QItemSelectionModel::currentRowChanged, this, [=](const QModelIndex ¤t, const QModelIndex &previous) { const QAbstractItemModel *model = current.model(); QModelIndex indexMaster = model->index(current.row(), static_cast<int>(ColumnsProgram::MODEL_COL_ID)); int idMaster = model->data(indexMaster).toInt(); _modelDetails->setFilter(QString("idMaster=%1").arg(idMaster)); });
When the user press a
QPushButton
I add a new row in the details table:_modelDetails->insertRow(_modelDetails->rowCount()); QModelIndex index = _modelDetails->index(_modelDetails->rowCount() - 1, 0); if (!index.isValid()) return; ui->viewDetails->setCurrentIndex(index); ui->viewDetails->edit(index);
All this stuff works, but when I press enter after populating the row the details model is not reselected:
I'm aware that using the
OnManualSubmit
strategy it's up to me when selecting again the model (after commit to the database). But with theOnRowChange
strategy I'm not sure how to detect when I have to call theselect()
function. I didn't find a signal for this.I tried using the
dataChanged()
but it fires every time I change the current cell even during editing.
And all the other signals arebefore
-something (i.e.beforeInsert
,beforeUpdate
, etc...).What is the correct way to handle this scenario?
The master/detail example is based upon theOnManualSubmit
strategy.