代码说得更好:
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位,g ++作为编译器
答案 0 :(得分:0)
来自QSqlTableModel::removeRows
文档:
在删除行之前发出beforeDelete()信号。编辑时 策略是OnManualSubmit信号发射延迟到 调用submitAll()。
请检查您正在使用的修改策略,并尝试在更改后调用model->submitAll()
。