PDA

View Full Version : How to store combobox value (not currentIndex) in database?



panoss
8th February 2017, 11:34
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:
12327


// 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);

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,
QSqlRelation("clients", "id", "lastname"));


So, how can I do this?
To store the id of the selected row?