PyQt5: Very slow QSortFilterProxyModel(not customized)
-
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... -
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)
-
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