如何通过单击按钮在QTableView中附加数据?

时间:2018-02-05 15:52:38

标签: python pyqt4 qtableview

我正在使用QTableView来显示我的数据库中的内容。该计划有2个QLineEditsself.nameSlotself.amountSlot。我的self.toDay也是QdateEdit。当我使用self.applyButton将新数据插入数据库时​​,它可以根据需要工作,但数据不会附加到QTableView。我该如何解决这个问题?这是我的代码

import sys
from PyQt4 import QtGui, QtCore, QtSql
from Printer import PrintView
from database import DatabaseInfo
import Sky2


class Sky(QtGui.QMainWindow, Sky2.Ui_MainWindow):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.setupUi(self)

        self.database = DatabaseInfo()
        self.database.setup()

        self.applyButton.clicked.connect(self.new_client)

        self.toDay.setMinimumDate(QtCore.QDate.currentDate())
        self.toDay.setMaximumDate(QtCore.QDate.currentDate())

        self.tableView.resizeColumnsToContents()
        self.tableView.horizontalHeader().setVisible(True)
        self.tableView.setShowGrid(False)
        self.tableView.setSortingEnabled(True)
        # failed to use sort
        # self.tableView.sortByColumn(self, 2, QtCore.Qt.SortOrder(0))
        database = QtSql.QSqlDatabase.addDatabase('QSQLITE')
        database.setDatabaseName('database.db')
        database.open()
        self.table_model = QtSql.QSqlQueryModel()
        self.table_model.setQuery("SELECT * FROM Clients ")
        self.tableView.setModel(self.table_model)
        self.table_model.setHeaderData(0, QtCore.Qt.Horizontal, "Name")
        self.table_model.setHeaderData(1, QtCore.Qt.Horizontal, "Amount")
        self.table_model.setHeaderData(2, QtCore.Qt.Horizontal, "Date")

    def new_client(self):
        name = self.nameSlot.text()
        amount = self.amountSlot.text()
        date = self.toDay.text()
        self.database.add_client(name, amount, date)
        self.table_model.layoutChanged.emit()
        self.tableView.setModel(self.table_model)
        # print('success')

   def keyPressEvent(self, event):
        if event.key() == QtCore.Qt.Key_P:
            self.printing()

    def printing(self):
        printer = QtGui.QPrinter(QtGui.QPrinter.ScreenResolution)
        dialog = QtGui.QPrintPreviewDialog(printer)
        view = PrintView()
        view.setModel(self.tableView.model())
        dialog.paintRequested.connect(view.print_)
        dialog.exec_()


def main():
    app = QtGui.QApplication(sys.argv)
    gui = Sky()
    gui.show()
    app.exec_()
if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:1)

QSqlQueryModel未收到有关更改的通知,因此如果您想获取新信息,则必须通过setQuery()

申请
def new_client(self):
    name = self.nameSlot.text()
    amount = self.amountSlot.text()
    date = self.toDay.text()
    self.database.add_client(name, amount, date)
    self.table_model.setQuery("SELECT * FROM Clients ")

如果您要使用表格,可以使用QSqlTableModel以简单且可选的方式使用QSqlRecord(),并且可以通过insertRecord()class Sky(QtGui.QMainWindow, Sky2.Ui_MainWindow): def __init__(self): QtGui.QMainWindow.__init__(self) self.setupUi(self) self.applyButton.clicked.connect(self.new_client) self.toDay.setMinimumDate(QtCore.QDate.currentDate()) self.toDay.setMaximumDate(QtCore.QDate.currentDate()) self.tableView.resizeColumnsToContents() self.tableView.horizontalHeader().setVisible(True) self.tableView.setShowGrid(False) self.tableView.setSortingEnabled(True) # failed to use sort # self.tableView.sortByColumn(self, 2, QtCore.Qt.SortOrder(0)) self.database = QtSql.QSqlDatabase.addDatabase('QSQLITE') self.database.setDatabaseName('database.db') if not self.database.open(): self.close() self.table_model = QtSql.QSqlTableModel() self.table_model.setTable("Clients") self.table_model.select() self.tableView.setModel(self.table_model) self.table_model.setHeaderData(0, QtCore.Qt.Horizontal, "Name") self.table_model.setHeaderData(1, QtCore.Qt.Horizontal, "Amount") self.table_model.setHeaderData(2, QtCore.Qt.Horizontal, "Date") def new_client(self): name = self.nameSlot.text() amount = self.amountSlot.text() date = self.toDay.text() record = self.table_model.record() record.setValue("Name", name) record.setValue("Amount", amount) record.setValue("Date", date) self.table_model.insertRecord(self.table_model.rowCount(), record) 方法添加元素,方法模型被通知,因为订单是由模型完成的。

groovy.text.SimpleTemplateEngine