Uploaded image for project: 'Qt for Python'
  1. Qt for Python
  2. PYSIDE-1951

Recursion while using QTreeWidgetItem.__lt__

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P3: Somewhat important
    • 6.5.2
    • 5.15.2
    • PySide
    • None
    • Windows

    Description

      Hi,

      I want to provide a custom sort order for QTreeWidgetItems withing the QTreeWidget.

      According to documentation, I can use the lt method: https://doc.qt.io/qtforpython-5/PySide2/QtWidgets/QListWidgetItem.html#PySide2.QtWidgets.PySide2.QtWidgets.QListWidgetItem.lt

      However, in practice, this gets a bit more complicated than this:

      Here is my code :

      
      class RepoBranchInfoTreeItem(QTreeWidgetItem):
      
         def __lt__(self, other: 'QTreeWidgetItem') -> bool:
         col = self.treeWidget().sortColumn()
         if col != 1:
          # regular sorting
          return bool( QTreeWidgetItem.__lt__(self, other) )
      
         colTextSelf = self.text(col)
         colTextOther = other.text(col)
         if len(colTextSelf) and len(colTextOther) and colTextSelf[0].isdigit() and colTextOther[0].isdigit():
            # natural number sorting if we can
            return extractInt(colTextSelf) < extractInt(colTextOther)
      
       # regular sort strategy will compare strings and place all number starting strings before others
       return bool( super().__lt__(other) )
      

      It's pretty simple, the idea is to use a natural sort order on column 1, if column 1 content is only made of digits.

       

      The issue I have is the following :

       Traceback (most recent call last):
       File "C:\work\Multigit\Dev\src\mg_dialog_git_switch_delete_branch.py", line 111, in __lt__
       def __lt__(self, other: 'QTreeWidgetItem') -> bool:
       RecursionError: maximum recursion depth exceeded while calling a Python object
      

      It seems that eventhough I call super().lt(self, other) or QTreeWidgetitem.lt(self, other), instead of calling the lt of QTreeWidgetItem , I end up calling my own method.

      So, virtual method override seems to be broken, I can no longer call the parent method. Or rather, i call it but it eventually calls my own implementation.

      By the way, it works in PyQt5

      https://forum.qt.io/topic/136452/recursion-while-using-qtreewidgetitem-__lt__

      Attachments

        1. pyside1951.py
          1 kB
          Friedemann Kleint

        Issue Links

          For Gerrit Dashboard: PYSIDE-1951
          # Subject Branch Project Status CR V

          Activity

            People

              ctismer Christian Tismer
              bluebird75 Philippe Fremy
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated: