PDA

View Full Version : Populating a combo box from another relation



pcheng
27th December 2012, 12:06
Hello all,

I am trying to populate two combo boxes from another relation using the code below:


// Setup the model to use in the mapper
model = new QSqlRelationalTableModel(this);
model->setTable("Orders");
model->setRelation(7, QSqlRelation("customers", "CustomerNumber", "Name"));
model->setRelation(5, QSqlRelation("employee", "EmployeeNumber", "UserName"));
model->select();
Qt::SortOrder order = Qt::AscendingOrder;
model->sort(0, order);

// Setup the mapper for the order widgets
mapper = new QDataWidgetMapper(this);
mapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);
mapper->setModel(model);
mapper->setItemDelegate(new QSqlRelationalDelegate(model));
mapper->addMapping(ui->idLineEdit, 0);
mapper->addMapping(ui->fullfilledCheckBox,1);
mapper->addMapping(ui->fullfilledDateEdit, 2);
mapper->addMapping(ui->orderDateEdit, 3);
mapper->addMapping(ui->dueDateEdit, 4);
mapper->addMapping(ui->employeeComboBox, 5, "currentIndex");
mapper->addMapping(ui->dueTimeEdit, 6);
mapper->addMapping(ui->customerComboBox, 7, "currentIndex");

The attribute names and numbers are correct. The orders relation has employee number in column 5 and customer number in column 7. The code runs and shows the order mapped correctly but the combo boxes are empty.

Any idea why this is happening?

Thanks,

Pericles

pcheng
29th December 2012, 13:39
I managed to add the names in the qcombobox using the following code:


ui->customerComboBox->setModel(model->relationModel(7));
ui->customerComboBox->setModelColumn(1);

But now when I save I save the index of the combo box instead of the actual value that relates to the name shown. Anyone knows how I can save the Customer Number (stored in column 0) in the customer table rather than the combobox index when I submit the model?

wysota
30th December 2012, 11:02
Shouldn't the comboboxes also have the relational delegate applied?

pcheng
2nd January 2013, 10:50
I decided to keep it simple now until I figure out how to do the combo boxes. I used a LineEdit and it works fine. The only difference is that I do not have a dropdown but I had to implement a Find function next to it that inserts the value.

Wysota: How should I apply the relational delegate to the combobox?

wysota
2nd January 2013, 13:37
Wysota: How should I apply the relational delegate to the combobox?

QComboBox::setItemDelegate()