Is it possible to sort numbers in a QTreeWidget column?
Asked Answered
A

5

8

I have a QTreeWidget with a column filled with some numbers, how can I sort them?

If I use setSortingEnabled(true); I can sort correctly only strings, so my column is sorted:

1 10 100 2 20 200

but this is not the thing I want! Suggestions?

Atrophy answered 12/12, 2008 at 16:10 Comment(0)
Q
12

You can sort overriding the < operator and changing sort condiction like this.

class TreeWidgetItem : public QTreeWidgetItem {
  public:
  TreeWidgetItem(QTreeWidget* parent):QTreeWidgetItem(parent){}
  private:
  bool operator<(const QTreeWidgetItem &other)const {
     int column = treeWidget()->sortColumn();
     return text(column).toLower() < other.text(column).toLower();
  }
};

In this example it ignore the real case, confronting fields in lowercase mode.

Quartana answered 14/12, 2008 at 15:10 Comment(1)
In order to sort numbers or even text containing numbers, QCollator can be used: QCollator collator; collator.setNumericMode( true ); return collator.compare( text( column ), other.text( column ) ) < 0;Longspur
R
6

Here's a pyQt implementation using __lt__

class TreeWidgetItem(QtGui.QTreeWidgetItem):

    def __init__(self, parent=None):
        QtGui.QTreeWidgetItem.__init__(self, parent)

    def __lt__(self, otherItem):
        column = self.treeWidget().sortColumn()
        return self.text(column).toLower() < otherItem.text(column).toLower()
Radicel answered 22/2, 2011 at 7:48 Comment(0)
B
4

The best way i found is to use a try block to find numbers

class TreeWidgetItem( QtGui.QTreeWidgetItem ):
    def __init__(self, parent=None):
        QtGui.QTreeWidgetItem.__init__(self, parent)

    def __lt__(self, otherItem):
        column = self.treeWidget().sortColumn()
        try:
            return float( self.text(column) ) > float( otherItem.text(column) )
        except ValueError:
            return self.text(column) > otherItem.text(column)
Bayadere answered 27/8, 2012 at 16:13 Comment(1)
This answer is quite complete, except for the > sign should be <.Shig
G
0

You could also insert the displayed text as integer:

for ( int i = 0; i < 1000; ++i )
{
   auto pTreeWidgetItem{ new QTreeWidgetItem( pTreeWidget ) };
   pTreeWidgetItem->setData( 0, Qt::DisplayRole, i + 1 );
}

In this case, the item will recognize the integer and will sort it correctly. The text displayed still is the number itself.

Girardi answered 19/9, 2024 at 15:21 Comment(0)
I
-1

numbers sort by numeric value, but strings sort the opposite way (i.e. "19999" < "2").

More specifically, strings are compared character by character from left to right until one one or the other characters differ, at which point the comparision is stopped. For instance, 19 and 121 will be compared like this:

"19"[0] != "121"[0] ? // no
"19"[1] != "121"[1] ? // yes
     '9' > '2' ?      // yes
          return some value that indicates "19" greater than "121";

To sort them correctly you will need to convert them to the numeric value and then sort them. Other than that you could implement your own sorting algorithm that reads numbers the correct way.

Intensive answered 12/12, 2008 at 16:22 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.