I have two tables, devices and clients (connected with relation devices.client_id=clients.id, that is client_id is the foreign key) and a form where I browse through the devices and select the client, as seen in the picture:
Tables_combo.png
// Create the model and set the table
model->setTable("devices");
clientIndex = model->fieldIndex("client_id");
// set the SQL relation
model->setRelation(clientIndex,
// populate the combo
ui.client_id_cbo->setModel(relClientModel);
ui.client_id_cbo->setModelColumn(relClientModel->fieldIndex("lastname"));
// mapping
mapper->setModel(model);
mapper->addMapping(ui.client_id_cbo, clientIndex);
// On insert new record
model->setData(model->index(row, typeIndex), type_cbo_item_data);
// Create the model and set the table
model = new QSqlRelationalTableModel(this);
model->setTable("devices");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
clientIndex = model->fieldIndex("client_id");
// set the SQL relation
model->setRelation(clientIndex,
QSqlRelation("clients", "id", "lastname"));
// populate the combo
QSqlTableModel *relClientModel = model->relationModel(clientIndex);
ui.client_id_cbo->setModel(relClientModel);
ui.client_id_cbo->setModelColumn(relClientModel->fieldIndex("lastname"));
// mapping
mapper = new QDataWidgetMapper(this);
mapper->setModel(model);
mapper->setItemDelegate(new QSqlRelationalDelegate(this));
mapper->addMapping(ui.client_id_cbo, clientIndex);
// On insert new record
model->setData(model->index(row, typeIndex), type_cbo_item_data);
To copy to clipboard, switch view to plain text mode
I want the variable 'type_cbo_item_data', line 25, to have the value of 4.
But the combo,I think, must hold (somehow) the ids from the table, from the
model->setRelation(clientIndex,
model->setRelation(clientIndex,
QSqlRelation("clients", "id", "lastname"));
To copy to clipboard, switch view to plain text mode
So, how can I do this?
To store the id of the selected row?
Bookmarks