代码说得更好:
model = new QSqlRelationalTableModel();
model->setEditStrategy(QSqlRelationalTableModel::OnRowChange);
model->setTable("members");
model->setRelation(Member_TeamID, QSqlRelation("teams", "ID", "Name"));
model->setSort(Member_Name, Qt::AscendingOrder);
model->select();
mapper = new QDataWidgetMapper();
mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
mapper->setModel(model);
mapper->setItemDelegate(new QSqlRelationalDelegate());
void member_detail::deleteMember()
{
int row = mapper->currentIndex();
bool x= model->removeRow(row);
mapper->submit();
mapper->setCurrentIndex(qMin(row, model->rowCount() - 1));
QMessageBox::critical(0,"W",QString::number(x)); // This Echos false
}
简单地说,当我调用 deleteMember 时,记录并没有从模型中删除,而是从数据库中删除(我使用 Navicat 对其进行检查)规格:Qt 5.0.2 Linux 64-bit,g++ 作为编译器