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.




Reply With Quote


Bookmarks