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:
model->setTable("unit");
model->select();
mapper->setModel(model);
mapper->addMapping(ui->le_Unit, model->fieldIndex("UnitName"));
mapper->addMapping(ui->le_AddressID, model->fieldIndex("AddressID"));
mapper->addMapping(ui->le_UnitID,model->fieldIndex("UnitID"));
connect(ui->pb_Prev,SIGNAL(clicked()),mapper, SLOT(toPrevious()));
connect(ui->pb_Next,SIGNAL(clicked()),mapper, SLOT(toNext()));
connect(mapper,SIGNAL(currentIndexChanged(int)),this,SLOT(mapperChanged(int)));
model = new QSqlTableModel;
model->setTable("unit");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();
mapper = new QDataWidgetMapper(this);
mapper->setModel(model);
mapper->addMapping(ui->le_Unit, model->fieldIndex("UnitName"));
mapper->addMapping(ui->le_AddressID, model->fieldIndex("AddressID"));
mapper->addMapping(ui->le_UnitID,model->fieldIndex("UnitID"));
connect(ui->pb_Prev,SIGNAL(clicked()),mapper, SLOT(toPrevious()));
connect(ui->pb_Next,SIGNAL(clicked()),mapper, SLOT(toNext()));
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:
addrmodel->setTable("addresses");
addrmodel->select();
addrMapper->setModel(addrmodel);
addrMapper->addMapping(ui->le_AID,addrmodel->fieldIndex("AddressID"));
addrMapper->addMapping(ui->le_FirstName,addrmodel->fieldIndex("FirstName"));
addrMapper->addMapping(ui->le_LastName,addrmodel->fieldIndex("LastName"));
addrmodel = new QSqlTableModel;
addrmodel->setTable("addresses");
addrmodel->setEditStrategy(QSqlTableModel::OnManualSubmit);
addrmodel->select();
addrMapper = new QDataWidgetMapper(this);
addrMapper->setModel(addrmodel);
addrMapper->addMapping(ui->le_AID,addrmodel->fieldIndex("AddressID"));
addrMapper->addMapping(ui->le_FirstName,addrmodel->fieldIndex("FirstName"));
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:
for(int row = 0; row < addrmodel->rowCount(); row++)
{
if(record.value(0).toInt() == ui->le_AddressID->text().toInt())
{
addrMapper->setCurrentIndex(row);
break;
}
}
for(int row = 0; row < addrmodel->rowCount(); row++)
{
QSqlRecord record = addrmodel->record(row);
if(record.value(0).toInt() == ui->le_AddressID->text().toInt())
{
addrMapper->setCurrentIndex(row);
break;
}
}
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 :
addrmodel->setFilter("AddressID='" + ui->le_AddressID->text() + "'");
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?
Bookmarks