如何通过单击按钮将数据附加到 QTableView 中?
How do i append data in a QTableView by clicking a button?
我正在使用 QTableView
来显示我数据库中的内容。该程序有 2 个 QLineEdits
:self.nameSlot
和 self.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()
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 ")
如果你打算使用table,你可以使用QSqlTableModel
以简单和可选的方式,你可以通过QSqlRecord()
和[=16添加元素=] 方法,使用此方法通知模型,因为订单由模型完成。
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)
我正在使用 QTableView
来显示我数据库中的内容。该程序有 2 个 QLineEdits
:self.nameSlot
和 self.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()
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 ")
如果你打算使用table,你可以使用QSqlTableModel
以简单和可选的方式,你可以通过QSqlRecord()
和[=16添加元素=] 方法,使用此方法通知模型,因为订单由模型完成。
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)