PDA

View Full Version : Troubleshooting QDataWidgetMapper to work with QComboBox



dyngoman
17th September 2012, 10:44
Good day to all,

I know this issue has been posted before but i could not find a suitable answer.
The problem is this :
I have a QDataWidgetMapper with the model set to a QSortFilterProxyModel that filters a QSqlRelationalTableModel.


model= new QSqlRelationalTableModel(this);
filter= new QSortFilterProxyModel(this);
filter->setSourceModel(model);
model->setTable("patients");
model->setRelation(3,QSqlRelation("doctors","doctor_id","doctor_name"));
model->select();


the data stored in the model comes from a" medical patients " tabel in witch the a field doctor_id is linked to primary key in doctors table like this

patients table :
================================================== =======
name [text] |condition [text] | treatment [text] |doctor_id
================================================== =======

doctors table:
==============================
[I]doctor_id | [I]doctor_name [text]
==============================

It's pretty straight forward right.
I filter the model by field name to make make the user's selection easier

Next I apply a mapper:


mapper = new QDataWidgetMapper(this);
mapper->setModel(filter);
mapper->setItemDelegate(new QSqlRelationalDelegate(this));


I mapped all other field to text edit widgets but I needed a combo box to select a doctor_name


QSqlTableModel* doctors= model->relationModel(model->fieldIndex("doctor_id "));
comboBox->setModel(tabelMedici);
comboBox->setModelColumn(tabelMedici->fieldIndex("doctor_name"));//populates the combo box with the names
mapper->addMapping(comboBox,model->fieldIndex("doctor_id "));

Finaly i need to navigate the model in a tableViewer


tableView->setModel(filter);
connect(tableView->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)) ,
mapper, SLOT(setCurrentModelIndex(QModelIndex)));

when i do a selection in tableView all the text widgets change values in accordance except comboBox witch doesn't change anything.

The combo box gets populated but is doesn't automatically change selection to the relevant field value.

What am I missing here?
Any suggestions?

wysota
17th September 2012, 13:00
You'll probably end up having to implement your own delegate for this with proper implementations of setModelData() and setEditorData().

dyngoman
18th September 2012, 09:07
OK ..so i figured it out. It seems that the mapper does not work properly on a sortfiltermodel , well at least not with the default QSqlRelationalDelegate.
Trying to reimplement QSqlRelationalDelegate to see what can be done.
I will post if results are favorable.