QSqlTableModel在InsertRecord之后进入编辑模式

时间:2018-10-27 14:30:11

标签: python python-3.x pyqt pyqt5

了解到此问题已在前面得到回答,因此使用的模型是QStandardItemModel。使用QSqlTableModel应用该解决方案不会获得相同的结果。所以问题是:插入记录后如何将QTableView置于编辑模式?下面的代码几乎可以将我带到那里,但是它将编辑第一个字段,但是随后的选项卡将其放置在下一个字段中,但是对于先前选择的记录,所以我认为我错过了一个步骤:

class SclDataBrowse(QWidget):
    def __init__(self, parent=None):
        super(SclDataBrowse, self).__init__(parent)
        self.resize(336, 462)
        self.setup_ui()
        self.setup_db()
        self.setup_model()
        self.view.setModel(self.model)
        if self.db.isOpen():
            self.load_data()

    def setup_ui(self):
        self.layout = QVBoxLayout(self)
        self.view = QTableView(self)
        self.btn_New = QPushButton(self)
        self.btn_New.setMinimumSize(QSize(70, 21))
        self.btn_New.setMaximumSize(QSize(70, 21))
        self.btn_New.setText('New')
        self.layout.addWidget(self.view)
        self.layout.addWidget(self.btn_New)

    def setup_db(self):
        self.db = QSqlDatabase("QMYSQL")
        self.db.setHostName(myhost)
        self.db.setUserName(myuser)
        self.db.setPassword(mypassword)
        self.db.setDatabaseName(mydb)
        self.db.open()

    def setup_model(self):
        self.model = QSqlTableModel(self,self.db)
        self.model.setTable("scldata")
        self.model.setEditStrategy(QSqlTableModel.OnManualSubmit)
        self.model.setHeaderData(0, Qt.Horizontal, 'ID')
        self.model.setHeaderData(1, Qt.Horizontal, 'Foreign ID')
        self.model.setHeaderData(2, Qt.Horizontal, 'Receipts Start')
        self.model.setHeaderData(3, Qt.Horizontal, 'Receipts End')
        self.model.setHeaderData(4, Qt.Horizontal, 'Billing Rate')

    def load_data(self):
        rec = QSqlRecord()
        rec.append(QSqlField("recordid",QVariant.String))
        rec.append(QSqlField("svcdataid",QVariant.String))
        rec.setValue('recordid',str(uuid.uuid4()))
        rec.setValue('svcdataid', "ParentID")
        row = self.model.rowCount()
        self.model.insertRecord(row,rec)
        ndx = self.model.index(row, 2)
        self.view.edit(ndx)

1 个答案:

答案 0 :(得分:1)

除了在QSqlRecord中建立新的currentIndex之外,还必须使用模型提供的相同selectionModel

from PyQt5 import QtCore, QtWidgets, QtSql
import uuid

myhost = ""
myuser = ""
mypassword = ""
mydb = ""

class SclDataBrowse(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(SclDataBrowse, self).__init__(parent)
        self.resize(336, 462)
        self.setup_ui()
        self.setup_db()
        self.setup_model()
        self.view.setModel(self.model)
        if self.db.isOpen():
            self.load_data()

    def setup_ui(self):
        self.view = QtWidgets.QTableView()
        self.btn_New = QtWidgets.QPushButton('New')
        self.btn_New.setFixedSize(70, 21)

        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.view)
        layout.addWidget(self.btn_New)

    def setup_db(self):
        self.db = QtSql.QSqlDatabase("QMYSQL")
        self.db.setHostName(myhost)
        self.db.setUserName(myuser)
        self.db.setPassword(mypassword)
        self.db.setDatabaseName(mydb)
        self.db.open()

    def setup_model(self):
        self.model = QtSql.QSqlTableModel(self,self.db)
        self.model.setTable("scldata")
        self.model.setEditStrategy(QtSql.QSqlTableModel.OnManualSubmit)
        titles = ('ID', 'Foreign ID', 'Receipts Start', 'Receipts End', 'Billing Rate')
        for i, title in enumerate(titles):
            self.model.setHeaderData(i, QtCore.Qt.Horizontal, title)

    def load_data(self):
        self.model.select()
        rec = self.model.record()
        rec.setValue('recordid',str(uuid.uuid4()))
        rec.setValue('svcdataid', "ParentID")
        row = self.model.rowCount()
        self.model.insertRecord(row, rec)
        ndx = self.model.index(row, 2)
        self.view.edit(ndx)
        self.view.selectionModel().setCurrentIndex(ndx, QtCore.QItemSelectionModel.Select)

if __name__ == '__main__':
    import sys

    app = QtWidgets.QApplication(sys.argv)
    combo = SclDataBrowse()
    combo.showMaximized()    
    sys.exit(app.exec_())