PyQt5 QSqlTableModel行插入后取消

时间:2018-10-31 16:03:20

标签: python python-3.x pyqt pyqt5

根据QSQLTableModel的文档,从模型中删除一行不会在提交事务之前从表视图中删除该行。我可以解决这个问题;但是,当我插入新行然后取消编辑时,我需要从模型和视图中删除该行。关于如何实现此目标的任何建议?

class SclDataModel(QSqlTableModel):
    def __init__(self, parent=None):
        super(SclDataModel, self).__init__(parent)
        self.setTable("scldata")
        self.setEditStrategy(QSqlTableModel.OnManualSubmit)
        self.setHeaderData(0, Qt.Horizontal, 'recordid')
        self.setHeaderData(1, Qt.Horizontal, 'svcdataid')
        self.setHeaderData(2, Qt.Horizontal, 'Receipts Start')
        self.setHeaderData(3, Qt.Horizontal, 'Receipts End')
        self.setHeaderData(4, Qt.Horizontal, 'Billing Rate')
        self.action = 0

    def revert(self):
        if self.action ==1   
            # How TO Remove the inserted row from the view????


class SclDataBrowse(QWidget):
    def __init__(self, parent=None):
        super(SclDataBrowse, self).__init__(parent)

    ...

    def add_record(self):
        rec = self.model.record()
        rec.setValue('recordid', str(uuid.uuid4()))
        rec.setValue('svcdataid', self.parentid)
        row = self.model.rowCount()
        self.model.insertRecord(row, rec)
        ndx = self.model.index(row, 2)
        self.model.action = 1
        self.tbl_View.edit(ndx)
        self.tbl_View.selectionModel().setCurrentIndex(ndx, QItemSelectionModel.Select)

1 个答案:

答案 0 :(得分:1)

将还原方法更改为以下内容:

def revert(self):
     if self.action == 1:                
          self.removeRow(self.rowCount()-1)
          self.submitAll()