我正在使用 pyqt4 框架为数据库表单做一些显示。不幸的是,我在尝试按姓氏过滤和显示我的数据库时遇到了麻烦。假设数据库连接正常。还假设我的 tupleHeader 中有正确数量的项目,因为我对其他方法使用相同的 initializeModel 方法(如下面描述的 search() 函数,它工作正常。
我调用了 display() 函数,它工作得很好,但是当从 sourceModel 创建 proxyModel 并尝试使用我的搜索函数显示 proxyModel 时,我显示了空单元格。当我限制我的搜索以过滤我一半的数据库时,它显示了许多单元格(所以大部分都在工作)。但它不会显示数据库本身的任何内容。
下面是我的一些代码:
from PyQt4 import QtGui, QtCore, QtSql
self.caseSensitivity = QtCore.Qt.CaseInsensitive
self.syntax = QtCore.QRegExp.FixedString
def initializeModel(self, model):
model.setTable(self.table)
#model.setEditStrategy(QtSql.QSqlTableModel.OnManualSubmit)
b = 0
for a in self.tupleHeader:
model.setHeaderData(b, QtCore.Qt.Horizontal, QtGui.qApp.tr(a))
b += 1
model.select()
def display(self):
'''reads all row data and displays it on a tableview'''
self.connectdb(self.db, self.localhost, self.dbname, self.username, self.password)
model = QtSql.QSqlTableModel()
self.initializeModel(model)
self.view.setModel(model)
self.disconnectdb(self.db)
def search(self, searchQuery):
'''queries database data, filters it, and displays it on a tableview'''
sourceModel = QtSql.QSqlTableModel()
proxyModel = QtGui.QSortFilterProxyModel()
self.initializeModel(sourceModel)
proxyModel.setSourceModel(sourceModel) # allows to edit proxyModel without changing underying model
#searchQuery contains the last name that I am filtering with
regExp = QtCore.QRegExp(searchQuery, self.caseSensitivity, self.syntax)
proxyModel.setFilterRegExp(regExp)
proxyModel.setFilterKeyColumn(2) # this column holds the last names
# self.view contains the table itemview my application uses to display the database
self.view.setModel(proxyModel)
编辑:我对保留这段代码不感兴趣,我只想知道为什么它允许表格显示表格的内容而不是一堆空单元格
print self.proxyModel.filterAcceptsRow(2, self.sourceModel)
此外,如果您在最后一条语句 ( self.view.setModel(proxyModel) ) 之后输入它,即使它确实发送了错误,它也会显示该表:
print self.proxyModel.filterAcceptsRow(2, self.sourceModel) TypeError: QSortFilterProxyModel.filterAcceptsRow(int, QModelIndex): argument 2 has unexpected type 'QSqlTableModel'
不管参数是什么,或者我是否使用 filterAcceptsRow ro filterAcceptsColumn,它都会显示表格。这是否缩小了问题的范围?
感谢您花时间搜索此编码错误/错误,祝您狩猎愉快!