PDA

View Full Version : advice on using QDataWidgetMapper in QtSql



schnitzel
5th June 2010, 06:49
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 = 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)),th is,SLOT(mapperChanged(int)));


The Address data widget mapper is configured like this:



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"));


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++)
{
QSqlRecord record = addrmodel->record(row);
if(record.value(0).toInt() == ui->le_AddressID->text().toInt())
{
addrMapper->setCurrentIndex(row);
break;
}

}


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() + "'");

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?

ChrisW67
5th June 2010, 07:23
According to the QDataWidgetMapper docs:
Every time the current index changes, each widget is updated with data from the model via the property specified when its mapping was made. I am guessing that changing (resetting) the underlying model is not changing the mapper's idea of the current index so it is not updating. Try a QDataWidgetMapper::toFirst() or QDataWidgetMapper::setCurrentIndex() to force it to think again.

schnitzel
5th June 2010, 07:33
According to the QDataWidgetMapper docs: I am guessing that changing (resetting) the underlying model is not changing the mapper's idea of the current index so it is not updating. Try a QDataWidgetMapper::toFirst() or QDataWidgetMapper::setCurrentIndex() to force it to think again.

You are right, this seems to work fine:


addrmodel->setFilter("AddressID='" + ui->le_AddressID->text() + "'");
addrMapper->toFirst();

thanks for the tip