change sort key on the class (not the instances)

This commit is contained in:
2023-02-02 11:59:05 +01:00
parent 3ba74a1a1e
commit c7c17ef221
2 changed files with 7 additions and 5 deletions

View File

@ -12,13 +12,16 @@ class DictListItem(QListWidgetItem):
self.key = key
self.value = value
self.set_alarm(False)
self._sort_key = None
def set_sort_key(self, sk):
self._sort_key = sk
_sort_key = None
@classmethod
def set_sort_key(cls, sk):
cls._sort_key = staticmethod(sk) # need to attach as staticmethod, otherwise self is inserted as first argument
def __lt__(self, other):
assert self._sort_key == other._sort_key
sk = self._sort_key
if sk:
return sk(self) < sk(other)

View File

@ -73,8 +73,7 @@ class DictListWidget(QListWidget):
def set_sort_key(self, sk):
for i in self.items.values():
i.set_sort_key(sk)
DictListItem.set_sort_key(sk)
if sk is None:
self.setSortingEnabled(False)