PDA

View Full Version : How to sort columns by "icon"?



youkai
30th July 2008, 13:31
Hi there. :)

I have a little problem to solve for my current project UltraStar Manager (http://sourceforge.net/projects/uman/).

Sorting in a QTreeWidget works fine as long as there is Text to sort. But how can I enable correct sorting for columns where only Icons appear?

These icons are "ticks" and "crosses". I would like to have "tick > cross" :) If you know what I mean...

So long

salmanmanekia
30th July 2008, 13:49
i am not sure about this..but did you try QIconEngineV2::read....http://doc.trolltech.com/4.4/qiconenginev2.html#read

mclark
30th July 2008, 17:52
When you create the QTreeWidgetItem for the items you want to sort, you could assign a data value to them and sort on that data value.


treeWidgetItem = new QTreeWidgetItem( parent );
treeWidgetItem->setData( column, Qt::DisplayRole, QVariant( tickValue ) );

As for sorting, override operator<() and you can do something like this.


bool YourClass::operator<( const QTreeWidgetItem& item ) const
{
bool bReturn = false;

switch ( this->column() )
{
case ICON_COLUMN:
if ( this->data( ICON_COLUMN, Qt::DisplayRole ).toUInt() < item.data( ICON_COLUMN, Qt::DisplayRole ).toUInt() )
bReturn = true;
break;
default:
bReturn = QTreeWidgetItem::operator <( item );
}

return bReturn;
}

I've used this with QTableWidgetItems with much success so it should translate to QTreeWidgetItems.

youkai
31st July 2008, 16:25
Thanks! I'll try it.

Edit: But I don't want to show any text or so. Maybe I can use the UserRole to store my "sort data" :)

2nd Edit: It worked very well! Thank you again.