python - 使用 QSqlTableModel 在 QTreeView 上存储期间处理更改的行

标签 python sqlite pyqt qsqltablemodel

我有一个QTreeView它通过QSqlTableModel显示来自SQLite的结果(PyQt5版本:5.13.0)。 我想通过详细对话框更改一些值,该对话框通过 modelTable.record() 填充数据。 因此我将编辑策略设置为OnManualSubmit并稍后使用 submitAll() 存储这些值.

问题:我使用 record.setValue() 将更改后的值写入 Db 。 但是:如果我更改一个值两次,当前行号可能会更改

重现:

  1. 运行下面的代码
  2. 双击最后一行名为“a”的单元格;将名称更改为“z”并且 按“保存”按钮。
  3. 对话框保持打开状态;最后一行成为第一行。 没关系,根据当前的排序顺序!现在更改条目 返回“a”并再次按“保存”按钮。

该条目将应用于最后一行。 -> 好的。

但是旧值仍然保留在第一行!现在我的第一行和最后一行完全相同,并且之前第一行的旧值丢失了! SQLite 错误 UNIQUE constraint failed: <id> Unable to fetch row也会发生。

预期:第二次按“保存”时,对话框中的值将显示在最后一行,并且第 1 行中的旧值是正确的。

(如果您按下“刷新”按钮或关闭 2 个修改操作之间的 dlg,您就会得到此信息。)

完整的工作代码示例:

import sys
import re
from PyQt5 import QtWidgets, QtGui, QtCore, QtSql

TRACE = True

MY_TABLE = "person"
db = QtSql.QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName(":memory:")

modelQuery = QtSql.QSqlQueryModel()
modelTable = QtSql.QSqlRelationalTableModel()

g_hits_count = 0
g_total_count = 0
g_selected_count = 0

def trace(message):
    if TRACE:
        print(message)

def _human_key(key):
    parts = re.split(r'(\d*\.\d+|\d+)', key)
    return tuple((e.swapcase() if i % 2 == 0 else float(e))
            for i, e in enumerate(parts))

class dlgDetail(QtWidgets.QDialog):        
    def __init__(self, parent, cur_row, row_id):
        QtWidgets.QDialog.__init__(self, parent)        
        self.setModal(False)
        self.setWindowModality(QtCore.Qt.NonModal)
        self.parent = self.parent()
        trace(f"dlgDetail was called with cur_row: {cur_row}")
        self.cur_row = cur_row
        self.row_id = row_id

        self.setupUi(self)

        # connect signals
        self.accepted.connect(self.dlgDetail_accepted)
        self.rejected.connect(self.dlgDetail_rejected)
        self.pbNew.clicked.connect(self.on_pbNew_clicked)
        self.pbSave.clicked.connect(self.on_pbSave_clicked)
        self.pbSave.setDefault(True)
        self.pbDel.clicked.connect(self.on_pbDel_clicked)
        self.pbCancel.clicked.connect(self.on_pbCancel_clicked)

        if cur_row > -1:  # existing record            
            # load selected record        
            # get current selection (could be filtered)
            sourceIdx = self.parent.treeView.model().mapToSource(self.parent.treeView.currentIndex())            
            self.cur_row = sourceIdx.row()
            trace(f"got my own cur_row -> {self.cur_row}")
            record_display = modelTable.record(self.cur_row)            

            persId = record_display.value("persId")
            lastName = record_display.value("lastName")
            firstName = record_display.value("firstName")
            country = record_display.value("name")

            # display record
            self.efId.setText(persId)        
            self.efLastName.setText(lastName)
            self.efFirstName.setText(firstName)
            self.populate_comboBox(self.cmbCountry, country)

        else:  # new record
            # get next free row id        
            modelQuery.setQuery("SELECT IFNULL(MAX(id),0)+1 FROM person")
            self.row_id = modelQuery.data(modelQuery.index(0, 0))

            self.efId.setText("new" + str(self.row_id))
            self.efLastName.setText("new" + str(self.row_id))
            self.efFirstName.setText("new" + str(self.row_id))
            self.populate_comboBox(self.cmbCountry, 0)

        self.show()

    def get_geometry(self):
        return self.geometry().x(), self.geometry().y(), self.geometry().width(), self.geometry().height()

    def center(self, offset):
        frameGm = self.frameGeometry()
        screen = QtWidgets.QApplication.desktop().screenNumber(QtWidgets.QApplication.desktop().cursor().pos())
        centerPoint = QtWidgets.QApplication.desktop().screenGeometry(screen).center()
        frameGm.moveCenter(centerPoint)
        self.move(frameGm.topLeft()+QtCore.QPoint(frameGm.topLeft().x(), frameGm.topLeft().y()+offset))

    def clear_inputs(self):
        self.efId.setText("new")        
        self.efLastName.setText("new")
        self.efFirstName.setText("new")
        self.cmbCountry.setCurrentIndex(0)

    def setupUi(self, parent):
        vBox = QtWidgets.QVBoxLayout(self)

        form = QtWidgets.QFormLayout()

        lblId = QtWidgets.QLabel(self)
        lblId.setText("ID:")              
        form.setWidget(0, QtWidgets.QFormLayout.LabelRole, lblId)

        self.efId = QtWidgets.QLineEdit(self)        
        form.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.efId)

        lblLastName = QtWidgets.QLabel(self)
        lblLastName.setText("Last Name:")        
        form.setWidget(1, QtWidgets.QFormLayout.LabelRole, lblLastName)

        self.efLastName = QtWidgets.QLineEdit(self)        
        form.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.efLastName)

        lblFirstName = QtWidgets.QLabel(self)
        lblFirstName.setText("First Name:")        
        form.setWidget(2, QtWidgets.QFormLayout.LabelRole, lblFirstName)

        self.efFirstName = QtWidgets.QLineEdit(self)                
        form.setWidget(2, QtWidgets.QFormLayout.FieldRole, self.efFirstName)

        lblCountry = QtWidgets.QLabel(self)
        lblCountry.setText("Country:")        
        form.setWidget(3, QtWidgets.QFormLayout.LabelRole, lblCountry)                    

        self.cmbCountry = QtWidgets.QComboBox(self)        
        form.setWidget(3, QtWidgets.QFormLayout.FieldRole, self.cmbCountry)

        vBox.addLayout(form)

        # command buttons
        hBox = QtWidgets.QHBoxLayout()

        spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)        
        hBox.addItem(spacerItem)

        self.pbNew = QtWidgets.QPushButton(self)
        self.pbNew.setText("New")
        hBox.addWidget(self.pbNew)        

        self.pbSave = QtWidgets.QPushButton(self)
        self.pbSave.setText("Save")
        hBox.addWidget(self.pbSave)

        self.pbDel = QtWidgets.QPushButton(self)
        self.pbDel.setText("Del")
        hBox.addWidget(self.pbDel)

        self.pbCancel = QtWidgets.QPushButton(self)
        self.pbCancel.setText("Cancel")
        hBox.addWidget(self.pbCancel)

        spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        hBox.addItem(spacerItem1)

        vBox.addLayout(hBox)

    def populate_comboBox(self, comboBox, country):        
        model = QtSql.QSqlTableModel()
        model.setTable("country")
        model.select()

        comboBox.setModel(model)
        comboBox.setModelColumn(model.fieldIndex("name"))

        # select the person's set country        
        index = comboBox.findText(str(country), QtCore.Qt.MatchFixedString)
        if index >= 0:            
            comboBox.setCurrentIndex(index)

    def dlgDetail_accepted(self):
        pass

    def dlgDetail_rejected(self):
        pass

    @QtCore.pyqtSlot()
    def on_pbCancel_clicked(self):        
        self.reject()

    @QtCore.pyqtSlot()
    def on_pbSave_clicked(self):
        trace(f"saving for row_id: {self.row_id}")
        country_id = self.cmbCountry.currentIndex()
        self.cur_row, self.row_id = self.parent.update_record(self.cur_row, self.row_id, self.efId.text(), self.efLastName.text(), self.efFirstName.text(), country_id)        

    @QtCore.pyqtSlot()
    def on_pbNew_clicked(self):
        self.accept()        
        self.parent.new_record()

    @QtCore.pyqtSlot()
    def on_pbDel_clicked(self):        
        self.parent.del_record()
        self.accept()        

class FilterHeader(QtWidgets.QHeaderView):
    filterActivated = QtCore.pyqtSignal()

    def __init__(self, parent):
        super().__init__(QtCore.Qt.Horizontal, parent)
        self._editors = []
        self._padding = 4
        self.setStretchLastSection(True)        
        self.setDefaultAlignment(QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter)
        self.setSortIndicatorShown(False)
        self.sectionResized.connect(self.adjustPositions)
        parent.horizontalScrollBar().valueChanged.connect(self.adjustPositions)

    def setFilterBoxes(self, count):
        while self._editors:
            editor = self._editors.pop()
            editor.deleteLater()
        for index in range(count):
            editor = QtWidgets.QLineEdit(self.parent())            
            editor.setPlaceholderText('Filter')
            editor.setClearButtonEnabled(True)            
            editor.textChanged.connect(self.textChanged)

            self._editors.append(editor)
        self.adjustPositions()

    def textChanged(self):        
        self.filterActivated.emit()

    def sizeHint(self):
        size = super().sizeHint()
        if self._editors:
            height = self._editors[0].sizeHint().height()
            size.setHeight(size.height() + height + self._padding)
        return size

    def updateGeometries(self):
        if self._editors:
            height = self._editors[0].sizeHint().height()
            self.setViewportMargins(0, 0, 0, height + self._padding)
        else:
            self.setViewportMargins(0, 0, 0, 0)
        super().updateGeometries()
        self.adjustPositions()

    def adjustPositions(self):
        for index, editor in enumerate(self._editors):
            height = editor.sizeHint().height()
            editor.move(
                self.sectionPosition(index) - self.offset() + 2,
                height + (self._padding // 2))
            editor.resize(self.sectionSize(index), height)

    def filterText(self, index):        
        if 0 <= index < len(self._editors):
            return self._editors[index].text()
        return ''

    def setFilterText(self, index, text):
        if 0 <= index < len(self._editors):
            self._editors[index].setText(text)

    def clearFilters(self):        
        for editor in self._editors:
            editor.clear()


class HumanProxyModel(QtCore.QSortFilterProxyModel):            
    def lessThan(self, source_left, source_right):
        data_left = source_left.data()
        data_right = source_right.data()
        if type(data_left) == type(data_right) == str:
            return _human_key(data_left) < _human_key(data_right)
        return super(HumanProxyModel, self).lessThan(source_left, source_right)    

    @property
    def filters(self):        
        if not hasattr(self, "_filters"):
            self._filters = []        
        return self._filters

    @filters.setter
    def filters(self, filters):
        self._filters = filters
        self.invalidateFilter()

        global g_hits_count
        g_hits_count = self.rowCount()

    def filterAcceptsRow(self, sourceRow, sourceParent):        
        for i, text in self.filters:            
            if 0 <= i < self.sourceModel().columnCount():
                ix = self.sourceModel().index(sourceRow, i, sourceParent)
                data = ix.data()                
                if text not in data:                
                    return False

        return True                

class winMain(QtWidgets.QMainWindow):
    cur_row = -1
    row_id = -1

    def __init__(self, parent=None):        
        super().__init__(parent)
        self.setWindowTitle("SqlTableModel Test")
        self.setupUi()
        self.setGeometry(300,200,700,500)

        self.treeView.selectionModel().selectionChanged.connect(self.item_selection_changed_slot)
        self.treeView.doubleClicked.connect(self.on_treeView_doubleClicked)
        self.center()
        self.show()        

    def closeEvent(self, event):                        
        self.deleteLater()

    def new_record(self):
        self.cur_row = -1
        self.row_id = -1

        dlgDetail(self, self.cur_row, self.row_id)

        self.updateStatus()        

    def update_record(self, cur_row, row_id, persId, lastName, firstName, country_id):
        trace("update_record() called. row: {cur_row}, id: {row_id}")
        record = modelTable.record()
        row = modelTable.rowCount()

        if cur_row > -1:  # existing entry
            trace(f"storing existing -> {lastName} (id: {row_id}) on row {cur_row}")
            row = cur_row            
            record.setValue("id", row_id)
        else:
            trace(f"storing new -> {lastName} (id: {row_id}) on row {cur_row}")
            row = modelTable.rowCount()            
            modelTable.insertRow(row) 

        record.setValue("persId", persId)
        record.setValue("lastName", lastName)
        record.setValue("firstName", firstName)
        record.setValue("name", country_id)

        # Update Model        
        modelTable.setRecord(row, record)        

        # Update Database
        if modelTable.submitAll():         
            trace("success on storing data.")            
        else:
            print("error on storing data: ", modelTable.lastError().text())

        if cur_row == -1:
            # get used row_id
            record_display = modelTable.record(row)
            row_id = record_display.value("persId")
            trace(f"   got new_row id from database: {row_id} after save.")

        self.updateStatus()

        # Remember current row data
        self.cur_row = row
        self.row_id = row_id

        print (f"returning row: {row}, row_id: {row_id}")

        return row, row_id

    def edit_record(self, sourceIdx):        
        if not sourceIdx:
            sourceIdx = self.treeView.currentIndex()

        # check if multiple selected rows exist
        if len(self.treeView.selectionModel().selectedRows()) > 1:            
            reply = QtWidgets.QMessageBox.question(self, f"Editing multiple records",
                                               f"Do you want to edit *all* the selected records?\rYes = Edit all records, No = Edit only current record",
                                               QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No | QtWidgets.QMessageBox.Cancel,
                                               QtWidgets.QMessageBox.Yes)
            if reply == QtWidgets.QMessageBox.Yes:
                # load all selected rows
                index_list = []                                                          
                for model_index in self.treeView.selectionModel().selectedRows():       
                    index = QtCore.QPersistentModelIndex(model_index)         
                    index_list.append(index)                                             

                offset = 0
                for index in index_list:                    
                    cur_row = index.row()
                    ix = self.treeView.model().index(index.row(), 0)  # column which contains the id            
                    row_id = ix.data()

                    dlgDetail(self, cur_row, row_id)
                    offset += 20
                return True

        # only single selection
        trace("single selection")
        self.cur_row = sourceIdx.row()
        ix = self.treeView.model().index(sourceIdx.row(), 0)  # column which contains the id            
        self.row_id = ix.data()

        dlgDetail(self, self.cur_row, self.row_id)

        self.updateStatus()

    @QtCore.pyqtSlot(QtCore.QModelIndex)
    def on_treeView_doubleClicked(self, index):        
        self.edit_record(index)

    def handleFilterActivated(self):        
        header = self.treeView.header()
        filters = []
        for i in range(header.count()):
            text = header.filterText(i)
            if text:
                filters.append((i, text))
        proxy = self.treeView.model()        
        proxy.filters = filters

        self.updateStatus()

    QtCore.pyqtSlot()
    def item_selection_changed_slot(self):
        selected = self.treeView.selectionModel()
        indexes = selected.selectedIndexes()

        global g_selected_count        
        g_selected_count = len(selected.selectedRows())

        sourceIdx = self.treeView.currentIndex()
        trace(f"sourceIdx.row() -> {sourceIdx.row()}")
        ix = self.treeView.model().index(sourceIdx.row(), 0)  # column which contains the id

        self.cur_row = sourceIdx.row()        
        self.row_id = ix.data()

        trace(f"setting cur_row to -> {self.cur_row}")
        trace(f"setting row_id to -> {self.row_id}")

        record = modelTable.record(self.cur_row)        

        persId = record.value("persId")
        lastName = record.value("lastName")
        firstName = record.value("firstName")
        country = record.value("name")

        trace(f"self.cur_row: {self.cur_row}")
        trace(f"self.row_id : {self.row_id}")
        trace(f"persId: {persId}")
        trace(f"lastName: {lastName}")

        self.updateStatus()

    def refresh(self):
        cur_row = -1
        row_id = -1
        self.clear_all_filters()        

        modelTable.select() # fetch the data of the table into the model

        self.treeView.sortByColumn(2, QtCore.Qt.DescendingOrder)

        self.updateStatus()

        self.statusBar.showMessage(f"data reloaded.")

    def updateStatus(self):
        global g_hits_count, g_total_count, g_selected_count


        sStatusText = f"Selected: {str(g_selected_count)} / Hits: {str(g_hits_count)} / Total: {str(g_total_count)}      |      Current row: {self.cur_row}, row id: {self.row_id}"

        self.statusBar.showMessage(sStatusText)

    def keyReleaseEvent(self, eventQKeyEvent):                
        key = eventQKeyEvent.key()
        modifiers = QtWidgets.QApplication.keyboardModifiers()
        if modifiers == QtCore.Qt.ShiftModifier and key == QtCore.Qt.Key_Escape:            
                self.clear_all_filters()

    def keyPressEvent(self, event):
        key = event.key()

        modifiers = QtWidgets.QApplication.keyboardModifiers()

        if modifiers != QtCore.Qt.ShiftModifier:
            focus_obj = self.focusWidget()

            if key == QtCore.Qt.Key_Return:  
                if type(focus_obj) == QtWidgets.QTreeView:
                    self.edit_record(self.treeView.currentIndex())


            elif key == QtCore.Qt.Key_Escape:            
                if type(focus_obj) == QtWidgets.QLineEdit:
                    focus_obj.clear()                

    def clear_all_filters(self):        
        # clear all inputs of type QLineEdit
        lineEdits = self.findChildren(QtWidgets.QLineEdit)
        for lineEdit in lineEdits:           
            lineEdit.clear()

    def center(self):
        frameGm = self.frameGeometry()
        screen = QtWidgets.QApplication.desktop().screenNumber(QtWidgets.QApplication.desktop().cursor().pos())
        centerPoint = QtWidgets.QApplication.desktop().screenGeometry(screen).center()
        frameGm.moveCenter(centerPoint)
        self.move(frameGm.topLeft())

    def setupUi(self):
        self.centralwidget = QtWidgets.QWidget(self)
        hBox = QtWidgets.QHBoxLayout(self.centralwidget)

        self.treeView = QtWidgets.QTreeView(self.centralwidget)       

        self.treeView.setRootIsDecorated(False)                       
        self.treeView.setSortingEnabled(True)
        self.treeView.setAlternatingRowColors(True)        
        self.treeView.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
        self.treeView.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
        self.treeView.header().setStretchLastSection(True)


        hBox.addWidget(self.treeView)
        self.setCentralWidget(self.centralwidget)

        header = FilterHeader(self.treeView)        
        self.treeView.setHeader(header)
        self.treeView.sortByColumn(2, QtCore.Qt.DescendingOrder)

        # StatusBar
        self.statusBar = QtWidgets.QStatusBar()
        self.setStatusBar(self.statusBar)

        # ToolBar
        exitAct = QtWidgets.QAction("Exit", self, shortcut="Ctrl+Q", triggered=QtWidgets.qApp.quit)        
        newRecordAct = QtWidgets.QAction("New", self, shortcut="Ctrl+N", triggered=self.new_record)
        editRecordAct = QtWidgets.QAction("Edit", self, shortcut="Return", triggered=self.edit_record)                
        refreshAct = QtWidgets.QAction("Refresh", self, shortcut="F5", triggered=self.refresh)        

        self.toolbar = self.addToolBar('Exit')
        self.toolbar.addAction(exitAct)
        self.toolbar.addSeparator()
        self.toolbar.addAction(newRecordAct)
        self.toolbar.addAction(editRecordAct)        
        self.toolbar.addSeparator()
        self.toolbar.addAction(refreshAct)

        modelTable.setTable(MY_TABLE)

        modelTable.setHeaderData(0, QtCore.Qt.Horizontal, "row id")
        modelTable.setHeaderData(1, QtCore.Qt.Horizontal, "ID")
        modelTable.setHeaderData(2, QtCore.Qt.Horizontal, "Last Name")
        modelTable.setHeaderData(3, QtCore.Qt.Horizontal, "First Name")
        modelTable.setHeaderData(4, QtCore.Qt.Horizontal, "Country")

        modelTable.setRelation(4, QtSql.QSqlRelation("country", "id", "name"))

        modelTable.setEditStrategy(QtSql.QSqlTableModel.OnManualSubmit)

        self.treeView.setModel(modelTable) # display data of the SQLTableModel into the QTreeView
        if not TRACE:
            self.treeView.setColumnHidden(0, True)

        # enable human sorting                
        proxy = HumanProxyModel(self)
        proxy.setSourceModel(modelTable)
        self.treeView.setModel(proxy)

        # enable filtering
        header.setFilterBoxes(modelTable.columnCount())
        header.filterActivated.connect(self.handleFilterActivated)

        # update counters
        global g_total_count, g_hits_count
        g_total_count = modelTable.rowCount()
        g_hits_count = g_total_count
        self.updateStatus()


def create_sample_data():     
    modelQuery.setQuery("""CREATE TABLE IF NOT EXISTS country (                                    
                                    id   INTEGER PRIMARY KEY UNIQUE NOT NULL,
                                    name TEXT
                                    )""")

    modelQuery.setQuery("""CREATE TABLE IF NOT EXISTS person (
                                   id         INTEGER PRIMARY KEY UNIQUE NOT NULL,
                                   persId     TEXT,
                                   lastName   TEXT,
                                   firstName  TEXT,
                                   country_id INTEGER NOT NULL DEFAULT 3,
              FOREIGN KEY (country_id) REFERENCES country(id)
                                   )""")

    # create some sample data for our model
    modelQuery.setQuery("INSERT INTO country (id, name) VALUES (0, 'None')")    
    modelQuery.setQuery("INSERT INTO country (id, name) VALUES (1, 'Angola')")    
    modelQuery.setQuery("INSERT INTO country (id, name) VALUES (2, 'Serbia')")
    modelQuery.setQuery("INSERT INTO country (id, name) VALUES (3, 'Georgia')")

    modelQuery.setQuery("INSERT INTO person (id, persId, lastName, firstName, country_id) VALUES (1, '1001', 'a', 'Robert', 1)")
    modelQuery.setQuery("INSERT INTO person (id, persId, lastName, firstName, country_id) VALUES (2, '1002', 'b', 'Brad', 2)")
    modelQuery.setQuery("INSERT INTO person (id, persId, lastName, firstName, country_id) VALUES (3, '1003', 'c', 'Angelina', 3)")

if __name__ == '__main__':                         
    app = QtWidgets.QApplication(sys.argv)             

    create_sample_data()

    window = winMain()    
    sys.exit(app.exec_())    

最佳答案

不要使用可视行,因为当行重新排序时它可能会发生变化,相反,您应该寻找一些持久元素,一个可能的解决方案是使用 QPersintenModelIndex。

在下面的代码中我实现了该逻辑:

class dlgDetail(QtWidgets.QDialog):
    def __init__(self, parent, index):
        super(dlgDetail, self).__init__(parent)
        self.setModal(False)
        self.setWindowModality(QtCore.Qt.NonModal)
        self.index = index
        self.parent = self.parent()
        trace(f"dlgDetail was called with cur_row: {index.row()}")

        self.setupUi(self)

        # connect signals
        self.accepted.connect(self.dlgDetail_accepted)
        self.rejected.connect(self.dlgDetail_rejected)
        self.pbNew.clicked.connect(self.on_pbNew_clicked)
        self.pbSave.clicked.connect(self.on_pbSave_clicked)
        self.pbSave.setDefault(True)
        self.pbDel.clicked.connect(self.on_pbDel_clicked)
        self.pbCancel.clicked.connect(self.on_pbCancel_clicked)

        if self.index.isValid():
            record_display = modelTable.record(self.index.row())

            persId = record_display.value("persId")
            lastName = record_display.value("lastName")
            firstName = record_display.value("firstName")
            country = record_display.value("name")

            self.efId.setText(persId)
            self.efLastName.setText(lastName)
            self.efFirstName.setText(firstName)
            self.populate_comboBox(self.cmbCountry, country)
        else:

            modelQuery.setQuery("SELECT IFNULL(MAX(id),0)+1 FROM person")
            self.row_id = modelQuery.data(modelQuery.index(0, 0))

            self.efId.setText("new" + str(self.row_id))
            self.efLastName.setText("new" + str(self.row_id))
            self.efFirstName.setText("new" + str(self.row_id))
            self.populate_comboBox(self.cmbCountry, 0)

        self.show()

    # ...

    @QtCore.pyqtSlot()
    def on_pbSave_clicked(self):
        self.parent.update_record(
            self.index,
            self.efId.text(),
            self.efLastName.text(),
            self.efFirstName.text(),
            self.cmbCountry.currentIndex()
        )
    # ...

class winMain(QtWidgets.QMainWindow):
    # ...

    def new_record(self):
        dlgDetail(self, QtCore.QPersistentModelIndex())
        self.updateStatus()

    def update_record(self, index, persId, lastName, firstName, country_id):
        if index.isValid():
            row = index.row()
            record = modelTable.record(row)
        else:
            row = modelTable.rowCount()
            modelTable.insertRow(row)
            record = modelTable.record()
        record.setValue("persId", persId)
        record.setValue("lastName", lastName)
        record.setValue("firstName", firstName)
        record.setValue("name", country_id)
        modelTable.setRecord(row, record)
        # Update Database
        if modelTable.submitAll():
            trace("success on storing data.")
        else:
            print("error on storing data: ", modelTable.lastError().text())

        if not index.isValid():
            record_display = modelTable.record(0)
            row_id = record_display.value("persId")
            trace(f"   got new_row id from database: {row_id} after save.")
        self.updateStatus()

    def edit_record(self, sourceIdx):

        if len(self.treeView.selectionModel().selectedRows()) > 1:
            reply = QtWidgets.QMessageBox.question(
                self,
                f"Editing multiple records",
                f"Do you want to edit *all* the selected records?\rYes = Edit all records, No = Edit only current recaord",
                QtWidgets.QMessageBox.Yes
                | QtWidgets.QMessageBox.No
                | QtWidgets.QMessageBox.Cancel,
                QtWidgets.QMessageBox.Yes,
            )
            if reply == QtWidgets.QMessageBox.Yes:
                for proxy_index in self.treeView.selectionModel().selectedRows():
                    proxy_model = proxy_index.model()
                    index = proxy_model.mapToSource(proxy_index)
                    p_index = QtCore.QPersistentModelIndex(index)
                    dlgDetail(self, p_index)
                return True

        if not sourceIdx:
            sourceIdx = self.treeView.currentIndex()
            if not sourceIdx.isValid():
                trace("not selection")
                return False
        # only single selection
        trace("single selection")
        proxy_model = sourceIdx.model()
        index = proxy_model.mapToSource(sourceIdx)
        dlgDetail(self, index)
        self.updateStatus()

完整的解决方案是 here

关于python - 使用 QSqlTableModel 在 QTreeView 上存储期间处理更改的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57201204/

相关文章:

python - Apache Oozie工作流程

SQLITE:无法删除未命名的主键

mysql - SQL从表b中列出表a的关联数据

数据库中的 SQLite 反向 LIKE 通配符

python - 使用集成的小部件。 PyQT 与 Tkinter

python - 如何找到子字符串并在 QTextEdit 中突出显示它?

Python:将 lambda 函数 append 到列表

Python - 比较两个 csv 文件中的重复值并将该行写入单独的 csv 文件中

python - 增加/减少 WAV 文件 Python 的播放速度

qt - QWebSocketServer 到不同的线程