QTreeWidget, setSortingEnabled
Hi,
I'm using several QTreeWidgets and activated now the setSortingEnabled. I have some problems with that function:
- when I enable the sorting, the widgets sorts automatically the first column in descending(!) order, why that? Can I change at least the default sort order to ascendng order?
- How can I remove the sorting so the normal order (the order I inserted the elements) is visible again?
- What would be the easiest way to sort a column that has text and numbers, it should sort the numbers like 1, 5, 10, 20 and not 1, 10, 20, 5
I do not want to use QTreeView and implement my own models because the QTreeWidget is already used in many places and I do not want to change all that code.
Thanks for any answer!
Cheers,
Rico
Re: QTreeWidget, setSortingEnabled
You can access to the header of QTreeView by calling header() then use QHeaderView::setSortIndicator ( int logicalIndex, Qt::SortOrder order ) to change the column and sort order. Or use QTreeView::sortByColumn( int column, Qt::SortOrder order )To remove sorting just set setSortingEnabled to false.
By default QTreeWidget sort items by text. So if you want to sort by int you have to subclass QTreeWidgetItem and reimplement bool QTreeWidgetItem::operator<(const QTreeWidgetItem &other) const
Re: QTreeWidget, setSortingEnabled
Quote:
Originally Posted by
osiris81
- What would be the easiest way to sort a column that has text and numbers, it should sort the numbers like 1, 5, 10, 20 and not 1, 10, 20, 5
Then set nembers to your items, then they are sorted like you want them to. E.g. don't use:
Code:
item->setText(4, "5");
use
Code:
item->setData(4,Qt::DisplayRole, 5);
Re: QTreeWidget, setSortingEnabled
thanks a lot for your help, that helped me!