PDA

View Full Version : QSqlRelationalTableModel and comboBox



musti
11th October 2007, 09:17
I am trying to understand the QSqlRelationalTableModel and playing with an example coming with qt4 examples/sql/relationaltablemodel
I want to add some properties to this example.
There are two comboboxes one of is country and the other is city. I want to see cities only in cityComboBox belong to chosed country in countryComboBox.
How can i do and if it possible I need an example.
Thanks

wysota
11th October 2007, 09:22
Your question is too general, looks like you want to add filtering, but I'm not sure. Try writing some code and post it here and we'll try to help you correct it if it doesn't work.

musti
12th October 2007, 22:25
I filled comboBoxCountry and comboBoxCity with another QSqlTableModel not with my main QSqlRelationalTableModel and than mapped
mapper->addMapping(comboBoxCity, cityIdx);
mapper->addMapping(comboBoxCountry, countryIdx);
I cannot filter the my main model. Filtering works or whole Sql query.
I added a signal to comboBoxCountry that it filters the city model. Every things ok.
If i submit changes to sql table, main country and city ids are updating to sql table form comboboxes. Because city and country indexes not changed in main model.
I must submit the mapper contents not model.
Any ideas?

wysota
12th October 2007, 22:53
Could we see something compilable?

musti
12th October 2007, 23:19
connect(comboBox_country, SIGNAL(activated(int)), this, SLOT(countryChanged(int)));
...
...
..
myModel1 = new QSqlTableModel(this);
myModel1->setTable("city");
myModel1->select();
comboBox_city->setModel(myModel1);
comboBox_city->setModelColumn(1);

myModel2 = new QSqlTableModel(this);
myModel2->setTable("country");
myModel2->select();
comboBox_country->setModel(myModel2);
comboBox_country->setModelColumn(1);
...
..

void myQtApp::countryChanged(int rownum)
{
QVariant country_id = myModel2->data(myModel2->index(rownum,0) );
myModel1->setFilter(tr("cid=%1").arg(country_id.toInt()));
}

modelling and mapping section

model= new QSqlRelationalTableModel(tableView);
model->setTable("person");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
cityIdx = model->fieldIndex("city");
countryIdx = model->fieldIndex("country");
...
...
model->setRelation(cityIdx, QSqlRelation("city", "id", "name"));
model->setRelation(countryIdx, QSqlRelation("country", "id", "name"));
mapper->addMapping(comboBox_city, cityIdx);
mapper->addMapping(comboBox_country, countryIdx);

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

I think i submit mapper changes to the model.

wysota
12th October 2007, 23:35
But what is mapper? QDataWidgetMapper? What do you need it for? Please provide a complete compilable project so that I can run it and see what your problem is.

musti
12th October 2007, 23:37
The example is in the following adress.
http://doc.trolltech.com/4.3/sql-relationaltablemodel.html
If i select Germany from Country combobox, i want to see only German cities in city combobox.

wysota
12th October 2007, 23:45
I know where the example is, but you paste some code which is not part of the example. As I said - if you want to see only German cities in the combobox, you have to do some filtering. You can do that by providing your own delegate that will filter out items, that don't have a specific country relation. But it's a bit complex change, so you can't base it on the above mentioned example.

musti
13th October 2007, 22:21
Forget my work. Because i must paste whole work to the form. My question is simple. If i change country, i want to see cities only that belong my chosen country in cityComboBox not other cities. Naturally i added country id to city table. I am asking only that if there is a simplest way to do this. Otherwise i do it manually.

wysota
14th October 2007, 09:43
As I said, you can implement your own delegate (as a subclass to QSqlRelationalDelegate) that will do the filtering inside QAbstractItemDelegate::setEditorData().