PDA

View Full Version : QSqlRelationalTableModel and editable QComboBox



cia.michele
5th October 2012, 17:54
Hello to everybody,
I read some post on this forum but i didn't found any answer to my problem. I hope you could help me :)
I've a table (Carichi) with a Foreing key of an other table (Colori), so I create my QSqlRelationalTableModel;


//Definizione del modello principale
myModel = new QSqlRelationalTableModel(this);
myModel->setTable("Carichi");
myModel->setEditStrategy(QSqlTableModel::OnManualSubmit);

Now I create the relationModel to a new QSqlTableModel:


int custIndexCol=myModel->fieldIndex("CodCol");
myModel->setRelation(custIndexCol,QSqlRelation("Colori","idColori","Colore"));

//Definizione del modello del colore
mdlCol = myModel->relationModel(custIndexCol);
mdlCol->setEditStrategy(QSqlTableModel::OnManualSubmit);
ui->cboColore->setModel(mdlCol);
ui->cboColore->setModelColumn(mdlCol->fieldIndex("Colore"));
ui->cboColore->setItemDelegate(new QSqlRelationalDelegate(ui->cboColore));


Now I've all my components (also a QComboBox to display items).

Now I'd like that the user can edit the QComboBox (setting it editable isn't it?) also adding a new element (that not is into Colori) and adding it to base table Colori, so it create the element for the foreign key and the table Carichi can save the new element created.

I thought that setting QComboBox editable could be enought, seen the model linked by setModel(), but, I understood that isn't!!

I hope I explain correctly enought my problem :)

Thanks a lot for your time

Michele

cia.michele
9th October 2012, 13:01
Hi guys, good news :)
I search on the net and I found some ideas nearly a QSqlRelationalDelegate, so I create a subclass and I modify the realationaltablemodel Qt example to test it. I attach my example so you can test it.

If you add a new city to Oslo, Munich and San Jose such as "New York", the city will be added to the list, but not immediatly selected for the employee record. But, if you select "New York" from the list, it will be saved into employee record :)

How can avoid the double selection of the city and select it for the employee record, at the first confirm after the adding of the city?

Thanks a lot for your time.

Michele