PDA

View Full Version : Sorting column in QTreeWidget



lni
14th November 2007, 17:58
Hi,

My column contains numerical values (1, 2, 3, ..., 99, 100,...)

When sort that column, the order is (1, 10, 100, 101, ..., 11, 12,...). Is there a way to sort in numerical order instead of alphabetic order?

Thanks.

DeepDiver
14th November 2007, 18:03
The only way I know of is to go for QTreeView and a Model, where the implementation of the method data() returns a QVariant containing an int or long.

wysota
14th November 2007, 18:16
The only way I know of is to go for QTreeView and a Model, where the implementation of the method data() returns a QVariant containing an int or long.
You still have a lot to learn before becoming a Master, young padawan ;)



When sort that column, the order is (1, 10, 100, 101, ..., 11, 12,...). Is there a way to sort in numerical order instead of alphabetic order?

Yes, subclass QTreeWidgetItem and reimplement its operator < to do the comparison based on numbers and not texts. You will probably also need to access the QTreeWidgetItem::treeWidget member to check which column is the active sorting column (QTreeWidget::sortColumn).

lni
14th November 2007, 18:24
The only way I know of is to go for QTreeView and a Model, where the implementation of the method data() returns a QVariant containing an int or long.

This is very bad. My design is based on QTreeWidget and QTreeWidgetItem.

I do see QTreeWidgetItem::setData ( int column, int role, const QVariant & value )

When using this method, I pass "int" into the QVariant, the ordering is still based on alphabetic...So I wonder if QTreeView and a Model will do the trick?

Or do you mean I need to do the sorting myself in the data() method? That is not acceptable either...

Thanks

DeepDiver
14th November 2007, 18:26
You still have a lot to learn before becoming a Master, young padawan ;)


Yes - master! I'll be willing to learn! :o

DeepDiver
14th November 2007, 18:31
When using this method, I pass "int" into the QVariant, the ordering is still based on alphabetic...So I wonder if QTreeView and a Model will do the trick?


Look at the impl:


bool QTreeWidgetItem::operator<(const QTreeWidgetItem &other) const
{
int column = view ? view->sortColumn() : 0;
return text(column) < other.text(column);
}

The operator is comparing the stringified data. That's why you get the column sorted alphabetic.

With wysota's solution you just need to re-impl this method to do the comparison on basis of the real data.

lni
14th November 2007, 18:35
Yes, subclass QTreeWidgetItem and reimplement its operator < to do the comparison based on numbers and not texts. You will probably also need to access the QTreeWidgetItem::treeWidget member to check which column is the active sorting column (QTreeWidget::sortColumn).

A little better, but still tedious... I wish they will take a comparator in QTreeWidget... Otherwise my general purpose MyTreeItem is no longer general purpose :mad:

But a least it seems to have a way...however, before I try it, can you please tell me which occurs first, that is, when user clicks column 3, will I get "3" as active column in the comparison operator? Just want to make sure I am not comparing the previous active column...

Thanks...

wysota
14th November 2007, 18:59
Everything will be ok. Just reimplement the operator. Resorting is caused by changing the sort column, not the other way round.

About the "tedious" argument - it is you who chose the tedious path by using QTreeWidget. The class is meant to be used for really simple cases, so basing a large application around QTreeWidget might have been a design error.

One more thing - you can implement sorting in a tree widget subclass yourself if you want. Just create a sorting slot and connect it to the header using signals.

lni
14th November 2007, 19:35
A little better, but still tedious... I wish they will take a comparator in QTreeWidget... Otherwise my general purpose MyTreeItem is no longer general purpose :mad:

But a least it seems to have a way...however, before I try it, can you please tell me which occurs first, that is, when user clicks column 3, will I get "3" as active column in the comparison operator? Just want to make sure I am not comparing the previous active column...

Thanks...

Thanks. I got it done by subclassing QTreeWidgetItem already :)

One observation I have is that I have to press mouse harder on the header section in order for the sorting to be triggered, otherwise it won't... Look like QTreeWidget have a new feature to detect the pressing force...
:o

Thanks guys!