QDataWidgetMapper将一个数据库记录字段反映到其映射的窗口部件中,同时将窗口部件中所做出的更改反映回数据库,关键是关联一个model和一组widget
一、步骤
1、创建 QDataWidgetMapper 对象
2、关联 model
3、关联 widgets,并创建其与model中section的映射
4、定位到某个record
QDataWidgetMapper *mapper = new QDataWidgetMapper;
mapper->setModel(model);
mapper->addMapping(mySpinBox, 0);
mapper->addMapping(myLineEdit, 1);
mapper->toFirst();
提交方式可以设为手动:
mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
QComboBox组件的mapper比较特殊
第一种、在关系模型中实现mapper到QComboBox组件
QSqlRelationalTableModel *model = QSqlRelationalTableModel(this);
model->setTable("员工表");
model->setRelation(dep_id,QSqlRelation("部门表","id","name"));
// ... 其它代码
//QComboBox与QListWidget很相