PDA

View Full Version : Sorting elements in QTableWidget



Elmo23x
23rd January 2007, 08:33
Hi to all!

I want to sort some elements in a QTableWidget.... the problem is that I do not know how to do it with a colum with numbers in a correct way. Now the result is something like this:

1
10
11
12
2
20
21
3
4
5
6

and I would like to have:

1
2
3
4
5
6
10
11
12
20
21


I hope that you can understand what I mean ;) Thanks for your help!!!

jpn
23rd January 2007, 08:49
Either set the numbers as integers, not as text or provide a custom QTableWidgetItem::operator<().


Approach 1:


int i = 0;
QTableWidgetItem* item = ...;
item->setText(QString::number(i)); // don't use this
item->setData(Qt::DisplayRole, i); // but this


Approach 2:
Subclass QTableWidgetItem, override QTableWidgetItem::operator<() (http://doc.trolltech.com/4.2/qtablewidgetitem.html#operator-lt).

crocus
23rd January 2007, 22:39
void sortAsc()
{
TableWidget->sortItems ( TableWidget->currentColumn() , Qt::AscendingOrder );
}
void sortDesc()

{
TableWidget->sortItems ( TableWidget->currentColumn() , Qt::DescendingOrder );
}

jpn
24th January 2007, 08:35
void sortAsc()
{
TableWidget->sortItems ( TableWidget->currentColumn() , Qt::AscendingOrder );
}
void sortDesc()

{
TableWidget->sortItems ( TableWidget->currentColumn() , Qt::DescendingOrder );
}

This does not make any difference because the items are in ascending order. They just need to be interpreted and sorted as numbers, not as text.