I have two tables: address and unit
unit table contains a field called 'AddressID'

address table contains primary key 'AddressID'

One address can occupy one ore more units.

The unit datawidget mapper is configure like this:
Qt Code:
  1. model = new QSqlTableModel;
  2. model->setTable("unit");
  3. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
  4. model->select();
  5.  
  6. mapper = new QDataWidgetMapper(this);
  7. mapper->setModel(model);
  8. mapper->addMapping(ui->le_Unit, model->fieldIndex("UnitName"));
  9. mapper->addMapping(ui->le_AddressID, model->fieldIndex("AddressID"));
  10. mapper->addMapping(ui->le_UnitID,model->fieldIndex("UnitID"));
  11.  
  12. connect(ui->pb_Prev,SIGNAL(clicked()),mapper, SLOT(toPrevious()));
  13. connect(ui->pb_Next,SIGNAL(clicked()),mapper, SLOT(toNext()));
  14. connect(mapper,SIGNAL(currentIndexChanged(int)),this,SLOT(mapperChanged(int)));
To copy to clipboard, switch view to plain text mode 

The Address data widget mapper is configured like this:

Qt Code:
  1. addrmodel = new QSqlTableModel;
  2. addrmodel->setTable("addresses");
  3. addrmodel->setEditStrategy(QSqlTableModel::OnManualSubmit);
  4. addrmodel->select();
  5. addrMapper = new QDataWidgetMapper(this);
  6. addrMapper->setModel(addrmodel);
  7. addrMapper->addMapping(ui->le_AID,addrmodel->fieldIndex("AddressID"));
  8. addrMapper->addMapping(ui->le_FirstName,addrmodel->fieldIndex("FirstName"));
  9. addrMapper->addMapping(ui->le_LastName,addrmodel->fieldIndex("LastName"));
To copy to clipboard, switch view to plain text mode 

In addition to this, I have a combo box where I have added all the unit name and unit id from the unit table so I can jump to any unit by calling mapperChanged with the index of the combo box.

This all works great, I can jump to any unit. My goal is to show the matching address and the only way I can make it work is like this:

Qt Code:
  1. for(int row = 0; row < addrmodel->rowCount(); row++)
  2. {
  3. QSqlRecord record = addrmodel->record(row);
  4. if(record.value(0).toInt() == ui->le_AddressID->text().toInt())
  5. {
  6. addrMapper->setCurrentIndex(row);
  7. break;
  8. }
  9.  
  10. }
To copy to clipboard, switch view to plain text mode 

However, this doesn't seem that efficient and I was hoping I could simply filter the addrmodel instead :

Qt Code:
  1. addrmodel->setFilter("AddressID='" + ui->le_AddressID->text() + "'");
To copy to clipboard, switch view to plain text mode 

But the addrMapper doesn't update. What am I doing wrong?

Is there a way to find the index of the model after the filter has been applied?