Hello everyone.

I am using the pyqt4 framework to do some displays for database forms. Unfortunately, I hit a snag while trying to filter and display my database by last name. Assume that the database connection works. Also assume that I have the correct amount of items in my tupleHeader since I use the same initializeModel method for other methods (like the search() function described below, and it works fine.

I call the display() function and it works perfectly fine, but when creating a proxyModel from the sourceModel, and trying to display the proxyModel with my search function, I have empty cells displayed. When I restrict my search so that it filters half my database, it shows that many cells (so most of this is working). But it will not display anything from the database itself.

Below is some of my code:
Qt Code:
  1. from PyQt4 import QtGui, QtCore, QtSql
  2.  
  3. self.caseSensitivity = QtCore.Qt.CaseInsensitive
  4. self.syntax = QtCore.QRegExp.FixedString
  5.  
  6. def initializeModel(self, model):
  7. model.setTable(self.table)
  8. #model.setEditStrategy(QtSql.QSqlTableModel.OnManualSubmit)
  9. b = 0
  10. for a in self.tupleHeader:
  11. model.setHeaderData(b, QtCore.Qt.Horizontal, QtGui.qApp.tr(a))
  12. b += 1
  13. model.select()
  14.  
  15.  
  16. def display(self):
  17. '''reads all row data and displays it on a tableview'''
  18. self.connectdb(self.db, self.localhost, self.dbname, self.username, self.password)
  19.  
  20. model = QtSql.QSqlTableModel()
  21. self.initializeModel(model)
  22. self.view.setModel(model)
  23.  
  24. self.disconnectdb(self.db)
  25.  
  26.  
  27. def search(self, searchQuery):
  28. '''queries database data, filters it, and displays it on a tableview'''
  29. sourceModel = QtSql.QSqlTableModel()
  30. proxyModel = QtGui.QSortFilterProxyModel()
  31.  
  32. self.initializeModel(sourceModel)
  33. proxyModel.setSourceModel(sourceModel) # allows to edit proxyModel without changing underying model
  34.  
  35. #searchQuery contains the last name that I am filtering with
  36. regExp = QtCore.QRegExp(searchQuery, self.caseSensitivity, self.syntax)
  37. proxyModel.setFilterRegExp(regExp)
  38. proxyModel.setFilterKeyColumn(2) # this column holds the last names
  39.  
  40. # self.view contains the table itemview my application uses to display the database
  41. self.view.setModel(proxyModel)
To copy to clipboard, switch view to plain text mode 

I am not interested in keeping this piece of code, I just want to know why allows the table to show the table's content instead of a bunch of empty cells

Qt Code:
  1. print self.proxyModel.filterAcceptsRow(2, self.sourceModel)
To copy to clipboard, switch view to plain text mode 

Also, if you put in this after the last statement ( self.view.setModel(proxyModel) ), it will show the table, even if it does send an error:

print self.proxyModel.filterAcceptsRow(2, self.sourceModel)
TypeError: QSortFilterProxyModel.filterAcceptsRow(int, QModelIndex): argument 2 has unexpected type 'QSqlTableModel'

It doesn't matter what the arguments are or whether I use filterAcceptsRow ro filterAcceptsColumn, it displays the table. Does this narrow down the problem some?

Thank you for your time searching for this coding error/bug, and happy hunting!