No, you have to subclass QTableWidgetItem, reimplement the operator and use your subclass instead of QTableWidgetItem.
No, you have to subclass QTableWidgetItem, reimplement the operator and use your subclass instead of QTableWidgetItem.
Raccoon29 (5th April 2008)
Ahi, my custom items seem to be ignored...
Here is some code:
CTableWidgetItem.h
Qt Code:
#include <QtGui> /** Subclass definition **/ { public: CTableWidgetItem(); bool operator<(const CTableWidgetItem &item); };To copy to clipboard, switch view to plain text mode
CTableWidgetItem.cpp
Qt Code:
#include "CTableWidgetItem.h" CTableWidgetItem::CTableWidgetItem() { } bool CTableWidgetItem::operator<(const CTableWidgetItem &item) { if(text().toUpper()<item.text().toUpper()) return true; else return false; }To copy to clipboard, switch view to plain text mode
then I used my CTableWidgetItem instead of QTableWidgetItem in the table, like this:
when I click the table's headbar they get sorted case insensitive like always.Qt Code:
QString id; int row=0; ... CTableWidgetItem *idvar=new CTableWidgetItem; idvar->setText(id); ui.tbwvariations->setItem(row,0,idvar); //tbwvariations is a GUI QTableWidgetTo copy to clipboard, switch view to plain text mode
I tried a MessageBox in the operator method, but it get no ever called, so application never passes through it.
What is the mistake...?![]()
--
raccoon29
"La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute"
You have to reimplement the existing operator< (the one that takes QTableWidgetItem), not implement yours that takes your item.
Raccoon29 (7th April 2008)
Also, notice the constness of the method.
J-P Nurmi
Raccoon29 (7th April 2008)
...right... now it works...
what a stupid mistake... Bjarne Stroustrup would hurt me...
Well, thank you both for the Nth time![]()
--
raccoon29
"La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute"
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
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".
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.
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().
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.
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.
Bookmarks