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.
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.
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
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).
metdos (25th September 2009)
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
Look at the impl:
The operator is comparing the stringified data. That's why you get the column sorted alphabetic.Qt Code:
{ int column = view ? view->sortColumn() : 0; return text(column) < other.text(column); }To copy to clipboard, switch view to plain text mode
With wysota's solution you just need to re-impl this method to do the comparison on basis of the real data.
metdos (25th September 2009)
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
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...
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.
Last edited by wysota; 14th November 2007 at 18:07.
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...
Thanks guys!
Bookmarks