Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. PyQt5: Very slow QSortFilterProxyModel(not customized)
Forum Updated to NodeBB v4.3 + New Features

PyQt5: Very slow QSortFilterProxyModel(not customized)

Scheduled Pinned Locked Moved Solved Qt for Python
3 Posts 2 Posters 1.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rhdr
    wrote on last edited by Rhdr
    #1

    How do you optimize the performance of a QSortFilterProxyModel based on the example code below?

    Example: In the example below I extract 200 records from the database, load them in a QSortFilterProxyModel & finally display them via a sort-able QTableView. Once the user click on a column header it takes about 20 seconds for 200 records to sort(?!) (If I sort the same amount of records in excel it takes less than a second)

    from PyQt5 import QtCore, QtWidgets, QtSql
    from models import _databaseConnection
            
    class FormController(QtWidgets.QMainWindow):
        def __init__(self):
            super(FormController, self).__init__()
            #connect to db
            _databaseConnection.connect()
            
            #setup model
            modelSource = QtSql.QSqlQueryModel()
            modelSource.setQuery("""SELECT TOP 200 Entity.Name, Entity.Surname
                                    FROM Entity""")
            
            #setup proxy
            modelProxy = QtCore.QSortFilterProxyModel()
            modelProxy.setSourceModel(modelSource)
            
            #setup ui
            self.resize(900, 700)
            tableView = QtWidgets.QTableView()
            tableView.setSortingEnabled(True)
            tableView.setModel(modelProxy)
            self.setCentralWidget(tableView)
            
    if __name__ == "__main__":
        import sys
        app = QtWidgets.QApplication(sys.argv)
        f = FormController()
        f.showNormal()
        sys.exit(app.exec_())
    

    Edit1:
    One work around to speed up the sorting is to simply re-query the database and request a sorted result set. The drawback however would be increased data usage for mobile users...

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Rhdr
      wrote on last edited by
      #2

      You right, I can do the initial/default sorting when loading the query as you suggested, but I still want the users to customize the sorting by clicking on the column headers should he/she wish to do so. Also I thought this is what QSortFilterProxyModel was designed for...

      How do you go about building a simple sortable index in qt? Are you referring to caching the data in something like a 2d-list and sorting that instead then afterwards updating the view? (Sorry, I did some searching on the net and could not find any reference to sortable indexes in qt)

      1 Reply Last reply
      0
      • R Offline
        R Offline
        Rhdr
        wrote on last edited by
        #3

        Seems like a balancing act either you increase the ram requirements or the app's data usage(front end vs back end processing). Will have to play with it and see which method is preferable.

        Either way it may go it seems back end processing would be a lot easier/faster to implement. I think for now I will run with it, get the app up and running and revisit the data usage issue in a future iteration if need be.

        Thank you

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved