QTableWidget case sensitive sort
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...
Re: QTableWidget case sensitive sort
Yes, reimplement operator< in QTableWidgetItem.
Re: QTableWidget case sensitive sort
Quote:
Originally Posted by
wysota
Thank you, brilliant idea :)
Re: QTableWidget case sensitive sort
oops... where should I overload it? :o
Have I to inherit from QTableWidget and replace the existing table (i'm using QtDesigner)? :eek:
Re: QTableWidget case sensitive sort
No, you have to subclass QTableWidgetItem, reimplement the operator and use your subclass instead of QTableWidgetItem.
Re: QTableWidget case sensitive sort
Quote:
Originally Posted by
wysota
Ok, you are right.
Thank you again :)
Re: QTableWidget case sensitive sort
Ahi, my custom items seem to be ignored...
Here is some code:
CTableWidgetItem.h
Code:
#include <QtGui>
/** Subclass definition **/
{
public:
CTableWidgetItem();
bool operator<(const CTableWidgetItem &item);
};
CTableWidgetItem.cpp
Code:
#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:
Code:
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:
Re: QTableWidget case sensitive sort
You have to reimplement the existing operator< (the one that takes QTableWidgetItem), not implement yours that takes your item.
Re: QTableWidget case sensitive sort
Also, notice the constness of the method.
Re: QTableWidget case sensitive sort
:o ...right... now it works...
what a stupid mistake... Bjarne Stroustrup would hurt me...
Well, thank you both for the Nth time :)
Re: QTableWidget case sensitive sort
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
Re: QTableWidget case sensitive sort
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".
Re: QTableWidget case sensitive sort
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.
Re: QTableWidget case sensitive sort
Quote:
Originally Posted by
jay
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
Re: QTableWidget case sensitive sort
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().
Re: QTableWidget case sensitive sort
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.
Re: QTableWidget case sensitive sort
Quote:
Originally Posted by
wysota
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.
Re: QTableWidget case sensitive sort
Quote:
Originally Posted by
aamer4yu
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.
Re: QTableWidget case sensitive sort
Dear wysota,
Thanks you sooo... much.
tableWidget ()->horizontalHeader () ->sortIndicatorOrder () - this helps me a lot to do proper sorting.
Thank you.
Re: QTableWidget case sensitive sort
this topic really help me,
I think post the modified code may help more.
Code:
//.h file
{
public:
CTableWidgetItem();
};
//.cpp file
CTableWidgetItem::CTableWidgetItem()
{
}
{
if(text().toUpper()<item.text().toUpper())
return true;
else
return false;
}