PDA

View Full Version : QTableWidget case sensitive sort



Raccoon29
3rd April 2008, 12:26
Hi all,
I have to sort a QTableWidget, and I achieve this with QTableView::setSortingEnabled(bool); but this sorting seems to be case sensitive, so the table is ordered in this way:
a....zA.....Z and so on.

Is there a way to make it NOT case sensitive? Maybe a flag or something like that...

wysota
3rd April 2008, 20:54
Yes, reimplement operator< in QTableWidgetItem.

Raccoon29
4th April 2008, 10:37
Yes, reimplement operator< in QTableWidgetItem.
Thank you, brilliant idea :)

Raccoon29
4th April 2008, 11:07
oops... where should I overload it? :o
Have I to inherit from QTableWidget and replace the existing table (i'm using QtDesigner)? :eek:

wysota
4th April 2008, 12:48
No, you have to subclass QTableWidgetItem, reimplement the operator and use your subclass instead of QTableWidgetItem.

Raccoon29
5th April 2008, 08:44
No, you have to subclass QTableWidgetItem, reimplement the operator and use your subclass instead of QTableWidgetItem.
Ok, you are right.
Thank you again :)

Raccoon29
5th April 2008, 09:56
Ahi, my custom items seem to be ignored...
Here is some code:
CTableWidgetItem.h


#include <QtGui>

/** Subclass definition **/
class CTableWidgetItem: public QTableWidgetItem
{
public:
CTableWidgetItem();
bool operator<(const CTableWidgetItem &item);
};


CTableWidgetItem.cpp


#include "CTableWidgetItem.h"

CTableWidgetItem::CTableWidgetItem()
{
}

bool CTableWidgetItem::operator<(const CTableWidgetItem &item)
{
if(text().toUpper()<item.text().toUpper())
return true;
else
return false;
}


then I used my CTableWidgetItem instead of QTableWidgetItem in the table, like this:


QString id;
int row=0;
...
CTableWidgetItem *idvar=new CTableWidgetItem;
idvar->setText(id);
ui.tbwvariations->setItem(row,0,idvar); //tbwvariations is a GUI QTableWidget

when I click the table's headbar they get sorted case insensitive like always.
I tried a MessageBox in the operator method, but it get no ever called, so application never passes through it.
What is the mistake...? :confused:

wysota
5th April 2008, 12:32
You have to reimplement the existing operator< (the one that takes QTableWidgetItem), not implement yours that takes your item.

jpn
5th April 2008, 18:43
Also, notice the constness of the method.

Raccoon29
7th April 2008, 10:37
:o ...right... now it works...

what a stupid mistake... Bjarne Stroustrup would hurt me...
Well, thank you both for the Nth time :)

jay
24th October 2008, 12:27
the above posts in this thread helped me to do sorting in case sensitive manner.
Thanks for this.

If an item contains both text and an icon,
is there any way to do sorting based on icon and also the text?
(In my application, i am displaying system files and folders. so i need to sort folders first, then files)

can anyone help me to do this.

I am using Qt4.4.0 in windows

wysota
24th October 2008, 12:29
The contents of the comparison operator is totally up to you, so you can sort based on any criteria you choose. In your case you need a mechanism to decide which icon is "smaller".

jay
25th October 2008, 11:46
Thanks a lot for your reply.

I have implemented this. and working fine.

can anyone help me to sort all other item except the first item ( I am having '..' as the first item).

after sorting, it is stayed in the first. but
problem is:
when the sorted column is again clicked, this comes to the bottom.

Lesiok
25th October 2008, 14:44
Thanks a lot for your reply.

I have implemented this. and working fine.

can anyone help me to sort all other item except the first item ( I am having '..' as the first item).

after sorting, it is stayed in the first. but
problem is:
when the sorted column is again clicked, this comes to the bottom.
For item '..' operator < must always return true

wysota
25th October 2008, 16:22
No, if the sort order is reversed, it has to be the greatest item of all, so it has to return false. I think it's best to reimplement QTableWidget::sortItems().

aamer4yu
27th October 2008, 06:54
I guess QTableWidget::sortItems() is not virtual, and cant be reimplemented , isnt it ??

Also for sorting icons, one can associate some role with the item and do sorting based on that role for the icon.

Lesiok
27th October 2008, 06:58
No, if the sort order is reversed, it has to be the greatest item of all, so it has to return false. I think it's best to reimplement QTableWidget::sortItems().

Oops, of course Wysota You are right.

wysota
27th October 2008, 08:07
I guess QTableWidget::sortItems() is not virtual, and cant be reimplemented , isnt it ??

Hmm... right. Somehow I was sure there was a "virtual" keyword when I checked the method before posting. Anyway, knowing the item, you can ask it for its QTableWidgetItem::tableWidget() which then can be asked for its QTableWidget::horizontalHeader() which in turn can be asked for QHeaderView::sortIndicatorOrder() and QHeaderView::sortIndicatorSection(). Based on that info you can implement the sorting function in the item.

jay
28th October 2008, 10:22
Dear wysota,

Thanks you sooo... much.

tableWidget ()->horizontalHeader () ->sortIndicatorOrder () - this helps me a lot to do proper sorting.

Thank you.

darksun190
23rd July 2013, 11:15
this topic really help me,
I think post the modified code may help more.


//.h file
class CTableWidgetItem: public QTableWidgetItem
{
public:
CTableWidgetItem();
CTableWidgetItem(const QString &text,int type= Type):QTableWidgetItem( text,type){};
virtual bool operator<(const QTableWidgetItem &item) const;
};
//.cpp file
CTableWidgetItem::CTableWidgetItem()
{
}

bool CTableWidgetItem::operator<(const QTableWidgetItem &item) const
{
if(text().toUpper()<item.text().toUpper())
return true;
else
return false;
}

prasad_N
15th December 2015, 07:18
If we are using proxy model, the simple one can be sortCaseSensitivity : http://doc.qt.io/qt-4.8/qsortfilterproxymodel.html#sortCaseSensitivity-prop