Hi, I have a table with two columns and approx. 50.000 rows of data. When I use the QSortFilterProxyModel, sorting is painfully slow.
I implemented a test model with one column and 500.000 string entries. When sorting with QSortFilterProxyModel, it takes approx. 5 seconds on my computer to sort. Using the builtin sort functions in Python it sorts instantly.
Is there any way to speed up the QSortFilterProxyModel? Or do I need to implement a custom sort method in all my models?

The test model looks like this, I hope it is correctly implemented, at least the correct index is returned when clicking on a row after sorting ;-)

Qt Code:
  1. import sys
  2.  
  3. from PyQt5.QtCore import *
  4. from PyQt5.QtGui import *
  5. from PyQt5.QtWidgets import *
  6.  
  7.  
  8. class MyModel(QAbstractTableModel):
  9.  
  10. def __init__(self, parent=None):
  11. super().__init__(parent)
  12. self._data = [str(i).rjust(6, '0') for i in range(500_000)]
  13.  
  14. def data(self, index: QModelIndex, role=None):
  15. if not index or not index.isValid():
  16. return QVariant()
  17. if index.column() == 0 and role == Qt.DisplayRole:
  18. return self._data[index.row()]
  19. return QVariant()
  20.  
  21. def rowCount(self, parent=None, *args, **kwargs):
  22. return len(self._data)
  23.  
  24. def columnCount(self, parent=None, *args, **kwargs):
  25. return 1
  26.  
  27. def headerData(self, column, orientation, role=None):
  28. if role == Qt.DisplayRole and orientation == Qt.Horizontal and column == 0:
  29. return 'Values'
  30. return QVariant()
  31.  
  32. def sort(self, column, order=None):
  33. self.layoutAboutToBeChanged.emit()
  34. if order == Qt.AscendingOrder:
  35. self._data.sort()
  36. else:
  37. self._data.sort(reverse=True)
  38. self.layoutChanged.emit()
  39. self.dataChanged.emit(QModelIndex(), QModelIndex())
  40.  
  41.  
  42. if __name__ == "__main__":
  43. app = QApplication(sys.argv)
  44.  
  45. view = QTableView()
  46. model = MyModel(view)
  47. view.setModel(model)
  48. view.resizeColumnsToContents()
  49. view.horizontalHeader().setStretchLastSection(True)
  50. view.setSortingEnabled(True)
  51. view.sortByColumn(0, Qt.AscendingOrder)
  52. view.show()
  53.  
  54. sys.exit(app.exec_())
To copy to clipboard, switch view to plain text mode 

But I wonder if it is possible to speed up the builtin Qt models, maybe there is a flag I'm not aware of?
Sorting 50.000 entries (or in this example 500.000 entries) should take that long?