Qt-interest Archive, December 2006
QTreeWidget sorting
Message 1 in thread
Using QTreeWidget with QTreeWidgetItems. Out of the
box, it looks like it sorts based on Ascii codes. We use it
as a selector but customers are complaining that ddd should not
be > ZZZ.
I see that there is a virtual operator< in QTreeWidgetItem but
it's seems like a lot to subclass this just to change this behavior.
Is there a setting to ignore case when sorting?
--
[ signature omitted ]
Message 2 in thread
Nope... Your going to have overload the less then operator. But the
method should be trivial. Also, you should look into using a natural
sort as well...
Since your customers will soon complain that the numbers are not getting
sorted as number, but text.
Scott
> -----Original Message-----
> From: Duane Hebert [mailto:spoo@xxxxxxxxx]
> Sent: Monday, December 18, 2006 7:09 AM
> To: qt-interest@xxxxxxxxxxxxx
> Subject: QTreeWidget sorting
>
> Using QTreeWidget with QTreeWidgetItems. Out of the
> box, it looks like it sorts based on Ascii codes. We use it
> as a selector but customers are complaining that ddd should not
> be > ZZZ.
>
>
> I see that there is a virtual operator< in QTreeWidgetItem but
> it's seems like a lot to subclass this just to change this behavior.
> Is there a setting to ignore case when sorting?
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
--
[ signature omitted ]
Message 3 in thread
"Scott Aron Bloom" <scott@xxxxxxxxxxxx> wrote in message
news:E2E5EA152B64E044B464DA705AF44AAE04F3A8@xxxxxxxxxxxxxxxxxxxxxx
>Nope... Your going to have overload the less then operator. But the
>method should be trivial. Also, you should look into using a natural
>sort as well...
Well sort of trivial. It's trivial to write a < operator that works with
the QTreeWidgetItem as long as you use the first column.
My tree widget has sorting enabled and has multiple columns.
It looks like the sort uses the < operator but somehow tied to the current
column.
This sort of works:
bool operator<(const QTreeWidgetItem &other)const {
int col = treeWidget()->currentColumn();
int x = QString::localeAwareCompare(text(col),other.text(col));
return x < 0;
}
I create a subclass of QTreeWidgetItem that has only a public
ctor and this overload.
This works fine if the user clicks in the column. But the user will most
likely click on the header item and this doesn't see the current column.
So I added a slot that fires on the headerview::itemClicked(int) signal
and does setCurrentItem(currentItem(),int );
This seems to work but like you say, a natural sort would
be better.
--
[ signature omitted ]