[PyQt] QSortFilterProxyModel: confusing with lessThan

Gottfried Müller gottfried.mueller at gmx.de
Tue Jul 30 09:15:25 BST 2019


Hello,

in a simple test I want to sort the items in an ascending order using my 
own proxy. I expected a comparision of "itemLeft.text() < 
itemRight.text()". But I had to code "itemRight.text() < 
itemLeft.text()" getting the ascending order.

Gottfried

Here is my example:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# pylint: disable=missing-docstring

import sys

from PyQt5.QtWidgets import QApplication, QTreeView
from PyQt5.QtGui import QStandardItemModel, QStandardItem
from PyQt5.QtCore import QSortFilterProxyModel


class MyProxy(QSortFilterProxyModel):

     def lessThan(self, mdlIdxSrcLeft, mdlIdxSrcRight):
         mdl = self.sourceModel()
         itemLeft = mdl.itemFromIndex(mdlIdxSrcLeft)
         itemRight = mdl.itemFromIndex(mdlIdxSrcRight)
         return itemRight.text() < itemLeft.text()

     # def filterAcceptsRow(self, srcRow, srcParentIdx):
     #    mdl = self.sourceModel()
     #    itemParent = mdl.itemFromIndex(srcParentIdx)
     #    if itemParent is None:
     #        srcItem = mdl.item(srcRow)
     #    else:
     #        srcItem = itemParent.child(srcRow)
     #    return srcItem.text().isnumeric()


class ApplWindow(QTreeView):

     def __init__(self, parent=None):
         super().__init__(parent)
         self.srcModel = QStandardItemModel(parent=self)
         self.proxy = MyProxy(parent=self)
         self.proxy.setSourceModel(self.srcModel)
         self.setModel(self.proxy)
         self.expandToDepth(0)
         self.setHeaderHidden(True)
         self.setAutoExpandDelay(500)
         self.setSortingEnabled(True)
         self.fillSrcModel()
         self.resize(200, 500)

     def fillSrcModel(self):
         sub = {0: "x2c", 1: "694ad", 2: "t8d2x"}
         for idx01, c01 in enumerate("a2b"):
             item01 = QStandardItem(c01)
             for c02 in sub[idx01]:
                 item02 = QStandardItem(c02)
                 item01.appendRow(item02)
             self.srcModel.appendRow(item01)
         self.expandToDepth(0)

def main():
     appl = QApplication(sys.argv)
     applWindow = ApplWindow()
     applWindow.show()
     return appl.exec_()


if __name__ == "__main__":
     main()



More information about the PyQt mailing list