QSqlRelationalTableModel :: removerow()从表中删除行,但不从模型中删除

时间:2013-06-20 14:48:40

标签: sql qt

代码说得更好:

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 ++作为编译器

1 个答案:

答案 0 :(得分:0)

来自QSqlTableModel::removeRows文档:

  

在删除行之前发出beforeDelete()信号。编辑时   策略是OnManualSubmit信号发射延迟到   调用submitAll()。

请检查您正在使用的修改策略,并尝试在更改后调用model->submitAll()