### 是否可以在QTreeWidget列中对数字进行排序？

1    10    100    2    20    200

#### 4 个答案:

``````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();
}
};
``````

``````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()
``````

``````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)
``````

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