Results 1 to 2 of 2

Thread: PyQt4: My database displays empty cells

  1. #1
    Join Date
    Jun 2010
    Qt products
    Unix/X11 Windows

    Default PyQt4: My database displays empty cells

    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
    3. self.caseSensitivity = QtCore.Qt.CaseInsensitive
    4. self.syntax = QtCore.QRegExp.FixedString
    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,
    12. b += 1
    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)
    20. model = QtSql.QSqlTableModel()
    21. self.initializeModel(model)
    22. self.view.setModel(model)
    24. self.disconnectdb(self.db)
    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()
    32. self.initializeModel(sourceModel)
    33. proxyModel.setSourceModel(sourceModel) # allows to edit proxyModel without changing underying model
    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
    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!

  2. #2
    Join Date
    Jun 2010
    Qt products
    Unix/X11 Windows

    Default Re: PyQt4: My database displays empty cells

    While I could not find the solution to my problem, it solved itself. I am not certain, but I think it was this code snippet that made it work.

    self.dbmanip = CoreDB(self.userTableView, self.table)

    This was put inside of the SetupUi() method created by the Qt4 Designer. I think either the dbmanip that contained the TableView lost the information from the proxyModel, or (more likely), I may have referenced the wrong table between the proxyModel and the original Model (that created the proxyModel), and then couldn't display because it was calling the cell structure from one table and the actual information from another.

    These are all guesses though. Still, problem solved.

Similar Threads

  1. Qt displays large size jpg
    By omegas in forum Qt Programming
    Replies: 14
    Last Post: 22nd April 2010, 05:07
  2. Filling empty cells in QTableWidget
    By lyucs in forum Newbie
    Replies: 2
    Last Post: 20th October 2009, 17:12
  3. Multiple displays on X11
    By alisami in forum Qt Programming
    Replies: 1
    Last Post: 25th September 2009, 17:25
  4. Default delegate displays empty QLineEdit?
    By andy.fillebrown in forum Qt Programming
    Replies: 3
    Last Post: 16th April 2009, 13:13
  5. remove directory empty or not empty
    By raphaelf in forum Newbie
    Replies: 12
    Last Post: 27th October 2006, 07:30

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.