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
Issue Links
- relates to
-
PYSIDE-1014 Custom sorting of QStandardItem cannot be implemented
- Closed
Gerrit Reviews
For Gerrit Dashboard: PYSIDE-1951 | ||||||
---|---|---|---|---|---|---|
# | Subject | Branch | Project | Status | CR | V |
412822,17 | shiboken6: Enable calling super().__lt__() in a Python class inheriting a C++ class with virtual operator<() | dev | pyside/pyside-setup | Status: NEW | 0 | 0 |