1
0

Enable sorting of list widgets

This commit is contained in:
Johann Bauer
2018-01-28 13:07:59 +01:00
committed by ThomasV
parent b2c7d2d2cc
commit 3965176295
4 changed files with 25 additions and 4 deletions

View File

@@ -763,6 +763,21 @@ def get_parent_main_window(widget):
return widget
return None
class SortableTreeWidgetItem(QTreeWidgetItem):
DataRole = Qt.UserRole + 1
def __lt__(self, other):
column = self.treeWidget().sortColumn()
if None not in [x.data(column, self.DataRole) for x in [self, other]]:
# We have set custom data to sort by
return self.data(column, self.DataRole) < other.data(column, self.DataRole)
try:
# Is the value something numeric?
return float(self.text(column)) < float(other.text(column))
except ValueError:
# If not, we will just do string comparison
return self.text(column) < other.text(column)
if __name__ == "__main__":
app = QApplication([])