[PyQt] QSortFilterProxyModel: confusing with lessThan

J Barchan jnbarchan at gmail.com
Tue Jul 30 09:32:22 BST 2019


On Tue, 30 Jul 2019 at 09:16, Gottfried Müller <gottfried.mueller at gmx.de>
wrote:

> 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()
>
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt
>

It should indeed be itemLeft.text() < itemRight.text() (for ascending
order) in lessThan(self, mdlIdxSrcLeft, mdlIdxSrcRight).  Yours will do
descending order.  I suggest you look again at your code/try a standalone
test.


-- 
Kindest,
Jonathan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190730/4383434b/attachment.html>


More information about the PyQt mailing list