Populating a combo box from another relation
Hello all,
I am trying to populate two combo boxes from another relation using the code below:
Code:
// Setup the model to use in the mapper
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->setModel(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
Re: Populating a combo box from another relation
I managed to add the names in the qcombobox using the following code:
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?
Re: Populating a combo box from another relation
Shouldn't the comboboxes also have the relational delegate applied?
Re: Populating a combo box from another relation
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?
Re: Populating a combo box from another relation
Quote:
Originally Posted by
pcheng
Wysota: How should I apply the relational delegate to the combobox?
QComboBox::setItemDelegate()