在PyQt5的QTableView中修复添加行功能

时间:2018-10-20 19:43:55

标签: python python-3.x pyqt pyqt5

这是我的代码,用于从数据库中获取数据并使用PyQt5创建表视图。基本上,我将其导入另一个文件并通过按钮调用它。输出正在获取我的数据库,并以正确的表格格式显示它。它正在正确更新。删除功能也可以正常工作。但是添加功能不起作用,导致应用崩溃。

import sys
from PyQt5 import QtSql, QtCore
from PyQt5.QtWidgets import *

class crud():

    def initializeModel(self,model):
        self.model.setTable('student')
        self.model.setEditStrategy(QtSql.QSqlTableModel.OnFieldChange)
        self.model.select()
        self.model.setHeaderData(0, QtCore.Qt.Horizontal, "Roll No")
        self.model.setHeaderData(1, QtCore.Qt.Horizontal, "Name")
        self.model.setHeaderData(2, QtCore.Qt.Horizontal, "Father Name")
        self.model.setHeaderData(3, QtCore.Qt.Horizontal, "Email")
        self.model.setHeaderData(4, QtCore.Qt.Horizontal, "Mobile")
        self.model.setHeaderData(5, QtCore.Qt.Horizontal, "Department")
        self.model.setHeaderData(6, QtCore.Qt.Horizontal, "Section")
        self.model.setHeaderData(7, QtCore.Qt.Horizontal, "Password")

    def createView(self,model):
        self.view=QTableView()
        self.view.setToolTip("Change the fields")
        self.view.setModel(model)
        return self.view

    def addrow(self,model):
        print(model.rowcount())
        ret=(model.insertRows(model.rowcount(),1))
        print(ret)

    def findrow(self,i):
        delrow=i.row()
        print(delrow)

    def datastudent(self):
        self.db=QtSql.QSqlDatabase.addDatabase('QSQLITE')
        self.db.setDatabaseName('atdata.db')
        self.model=QtSql.QSqlTableModel()
        delrow=-1
        self.initializeModel(self.model)

        self.View1=self.createView("Table Model",self.model)
        self.View1.clicked.connect(self.findrow)

        self.dlg=QDialog()
        self.layout=QVBoxLayout()
        self.layout.addWidget(self.View1)

        self.button=QPushButton("Add a row")
        self.button.clicked.connect(self.addrow)
        self.layout.addWidget(self.button)

        self.btn1=QPushButton("Delete a Row")
        self.btn1.clicked.connect(lambda :self.model.removeRow(self.View1.currentIndex().row()))
        self.layout.addWidget(self.btn1)

        self.dlg.setLayout(self.layout)
        self.dlg.setWindowTitle("My Database")
        self.dlg.setGeometry(100, 100, 1100, 600)
        self.dlg.exec_()

这是用于添加功能的代码,我正面临其中的问题。定义此功能是为了在数据库中添加新行以插入数据。

 def addrow(self,model):
        print(model.rowcount())
        ret=(model.insertRows(model.rowcount(),1))
        print(ret)

注意:-该代码正常工作,但是在我在类中定义它之前,因为我想导入它。我的原始代码是 here ,请查看以获取参考。 请帮忙。预先谢谢你。

1 个答案:

答案 0 :(得分:0)

将一组函数和语句修改为一个类的想法并非微不足道,主要问题之一是由于消除了方法中变量的范围而引起的,并且可能引起问题,例如单击一个按钮不传递任何参数,因此addrow的模型参数不存在,并且会引发该错误,并且您肯定会认为self.model与model相同,但事实并非如此。

所以我自由地将类转换为自定义QDialog:

import sys
from PyQt5 import QtCore, QtWidgets, QtSql 


class CrudDialog(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super(CrudDialog, self).__init__(parent)

        db = QtSql.QSqlDatabase.addDatabase('QSQLITE')
        db.setDatabaseName('atdata.db')

        self.view = QtWidgets.QTableView()
        self.view.setToolTip("Change the fields")
        self.view.setWindowTitle("Table Model")

        btn_add = QtWidgets.QPushButton("Add a row")
        btn_add.clicked.connect(self.add_row)

        btn_del = QtWidgets.QPushButton("Delete a row")
        btn_del.clicked.connect(self.remove_row)

        lay = QtWidgets.QVBoxLayout(self)
        lay.addWidget(self.view)
        lay.addWidget(btn_add)
        lay.addWidget(btn_del)

        self.model = QtSql.QSqlTableModel()
        self.model.setTable('student')
        self.model.setEditStrategy(QtSql.QSqlTableModel.OnFieldChange)
        self.model.select()
        self.model.setHeaderData(0, QtCore.Qt.Horizontal, "Roll No")
        self.model.setHeaderData(1, QtCore.Qt.Horizontal, "Name")
        self.model.setHeaderData(2, QtCore.Qt.Horizontal, "Father Name")
        self.model.setHeaderData(3, QtCore.Qt.Horizontal, "Email")
        self.model.setHeaderData(4, QtCore.Qt.Horizontal, "Mobile")
        self.model.setHeaderData(5, QtCore.Qt.Horizontal, "Department")
        self.model.setHeaderData(6, QtCore.Qt.Horizontal, "Section")
        self.model.setHeaderData(7, QtCore.Qt.Horizontal, "Password")
        self.view.setModel(self.model)

        self.resize(1100, 600)

    @QtCore.pyqtSlot()
    def add_row(self):
        self.model.insertRows(self.model.rowCount(), 1)

    @QtCore.pyqtSlot()
    def remove_row(self):
        self.model.removeRow(self.view.currentIndex().row())
        self.model.select()

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    button = QtWidgets.QPushButton("Show")
    c = CrudDialog()
    button.clicked.connect(c.exec_)
    button.show()
    sys.exit(app.exec_())

您不应再调用datastudent,而应调用exec _:

self.crud_dialog = CrudDialog()
some_button.clicked.connect(self.crud_dialog.exec_)