Z
Z
Zakhar Alekseenko2015-09-10 14:05:27
Database
Zakhar Alekseenko, 2015-09-10 14:05:27

QDataWidgetMapper not setting current value of QCombobox?

Hello.
I ran into a problem when trying to use combobox to edit a table field (ProgrammOptions). The contents of the combobox are loaded from 2 tables (Version). The tables are linked by the FK_Version foreign key in ProgrammOptions.
The essence of the problem: the fields of the Version table are loaded into the combobox and edited, but after loading the combobox is set to the very first value in the list, whichever was selected before.

//Заполняю модель
model2 = new QSqlRelationalTableModel();
model2->setEditStrategy(QSqlTableModel::OnFieldChange);
model2->setTable("ProgrammOptions"); /*Первая таблица*/
int idVersionFK = model2->fieldIndex("FK_Version"); /*Внешний ключ*/
model2->setRelation(idVersionFK, QSqlRelation("Version", "idVersion", "Name")); /*Устанавливаем связь со второй таблицей */

if ( !model2->select() ){
        qDebug()<<"No";
}
//Заполняю combobox данными из модели и говорю что показывать нужно колонку Name
ui->comboBox->setModel(model2->relationModel(idVersionFK));
ui->comboBox->setModelColumn(model2->relationModel(idVersionFK)->fieldIndex("Name"));

//Создаю маппер.
mapper = new QDataWidgetMapper();
mapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);
mapper->setOrientation(Qt::Horizontal);
mapper->setModel(model2);
mapper->addMapping(ui->checkBoxModbus, model2->fieldIndex("EnableModbus"));
mapper->addMapping(ui->checkBoKIABING3, model2->fieldIndex("EnableKIABING"));
mapper->addMapping(ui->checkBox3D, model2->fieldIndex("Enable3D"));
mapper->addMapping(ui->comboBox, model2->fieldIndex("Name"), "currentIndex"); /* Привязываемся по свойству currentIndex*/
mapper->toFirst();

The top table is Version
The bottom table is ProgramOptions
1dcda7cacb264fcda3f64bf4a58e5e5c.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
Zakhar Alekseenko, 2015-09-11
@Zaher220

A working solution has been found.
mapper->addMapping(ui->comboBox, model2->fieldIndex("Name"));
Unfortunately, there is still no understanding of why the binding by index does not work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question