jorge.saridis
28th September 2009, 14:59
Hi, I'm new to Qt4. I'm using Qt4.5
I use QSqlRelationalTableModel to show in a table view a database table with foreign keys to other tables. This works fine.
I created a form to edit each table row. I used a mapper and connected table selection model signal to setCurrentModelIndex mapper slot, this is the code
model = new QSqlRelationalTableModel(this, database);
model->setTable("centros_programas");
model->setRelation(1, QSqlRelation("provincias", "id_provincias", "nombre"));
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();
model->setHeaderData(0, Qt::Horizontal, QObject::tr("Id"));
model->setHeaderData(1, Qt::Horizontal, QObject::tr("Provincia"));
model->setHeaderData(2, Qt::Horizontal, QObject::tr("Centro/Programa"));
model->setHeaderData(3, Qt::Horizontal, QObject::tr("Tipo"));
ui->comboBoxProvincia->setModel(model->relationModel(1) );
ui->comboBoxProvincia->setModelColumn(model->relationModel(1)->fieldIndex("nombre"));
ui->table->setModel(model);
ui->table->setColumnHidden(0, true);
mapper = new QDataWidgetMapper(this);
mapper->setModel(model);
mapper->addMapping(ui->comboBoxProvincia, model->fieldIndex("id_provincias") );
mapper->addMapping(ui->lineEditCentro, model->fieldIndex("nombre") );
mapper->addMapping(ui->lineEditTipo, model->fieldIndex("tipo") );
mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
connect(ui->table->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)) , mapper, SLOT(setCurrentModelIndex(QModelIndex)));
the thing is: in the form for editing each row, I have a combo box with data from a related table, as you can see in this line:
ui->comboBoxProvincia->setModel(model->relationModel(1) );
I didn't create another model for the combo box.
When I see it running it fills correctly the combo box, but it is not synchronized with data, it always shows first row of the related table.
I was searching and it seems that I need a delegate for it. This is right? I will need a delegate for each combo box? What if I have two or more combo boxes in the form?
Thanks in advance
I use QSqlRelationalTableModel to show in a table view a database table with foreign keys to other tables. This works fine.
I created a form to edit each table row. I used a mapper and connected table selection model signal to setCurrentModelIndex mapper slot, this is the code
model = new QSqlRelationalTableModel(this, database);
model->setTable("centros_programas");
model->setRelation(1, QSqlRelation("provincias", "id_provincias", "nombre"));
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();
model->setHeaderData(0, Qt::Horizontal, QObject::tr("Id"));
model->setHeaderData(1, Qt::Horizontal, QObject::tr("Provincia"));
model->setHeaderData(2, Qt::Horizontal, QObject::tr("Centro/Programa"));
model->setHeaderData(3, Qt::Horizontal, QObject::tr("Tipo"));
ui->comboBoxProvincia->setModel(model->relationModel(1) );
ui->comboBoxProvincia->setModelColumn(model->relationModel(1)->fieldIndex("nombre"));
ui->table->setModel(model);
ui->table->setColumnHidden(0, true);
mapper = new QDataWidgetMapper(this);
mapper->setModel(model);
mapper->addMapping(ui->comboBoxProvincia, model->fieldIndex("id_provincias") );
mapper->addMapping(ui->lineEditCentro, model->fieldIndex("nombre") );
mapper->addMapping(ui->lineEditTipo, model->fieldIndex("tipo") );
mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
connect(ui->table->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)) , mapper, SLOT(setCurrentModelIndex(QModelIndex)));
the thing is: in the form for editing each row, I have a combo box with data from a related table, as you can see in this line:
ui->comboBoxProvincia->setModel(model->relationModel(1) );
I didn't create another model for the combo box.
When I see it running it fills correctly the combo box, but it is not synchronized with data, it always shows first row of the related table.
I was searching and it seems that I need a delegate for it. This is right? I will need a delegate for each combo box? What if I have two or more combo boxes in the form?
Thanks in advance