PDA

View Full Version : How to move QDataWidgetMapper to a specific record ?



marcvanriet
16th November 2010, 23:30
Hi,

I have a small database application that allows maintaining the member list of a sports club.

I have a form with QLineEdit's for the different fields. I have a QSqlTableModel to connect to the table. I use a QDataWidgetMapper to connect the linedits to the fields in the database.

Like this :

QSqlTableModel *model= new QSqlTableModel(this);
model->setTable("MemberList");
model->select();

mapper = new QDataWidgetMapper(this);
mapper->setModel(model);
mapper->addMapping(ui->leFirstName, model->fieldIndex("FirstName"));
mapper->addMapping(ui->leSurName, model->fieldIndex("SurName"));


I can move back and forward using the navigation methods of the mapper :

mapper->toNext();
mapper->setCurrentIndex( /* any number here */ );
}

But now I need to move to a specific record. For instance, the user can type a name in a filter box somewhere, and the corresponding record must be shown. (In reality I get a key value of the record when a member is selected in some other form in the application, and need to move to the record with that value in its 'MemberKey' field.)

Unfortunately the mapper or model have no methods for searching for a specific record.

I thought I could use setFilter() on the model, like below, but when I do this the program crashes.

model->setFilter( QString(" FirstName= 'Peter' ") );


Does anyone know how to move to a specific record ? I have no idea of the index number of the record to show, so I cannot use setCurrentIndex().

Best regards,
Marc

marcvanriet
17th November 2010, 20:51
OK... stupid mistake. The member variable for the model was not filled in, it was put into a local variable instead.

For those interested : just do a setfilter() to the record you want, and then do a toFirst().


model->setFilter( QString(" MemberKey = %1 ").arg(sKeyValue) );
mapper->toFirst();


Regards,
Marc