PDA

View Full Version : QTableWidget sorting question



tommy
30th May 2009, 10:54
I'm sorting a QTableWidget object with the sortItems() function.



QTableWidget myTable = new QTableWidget(rows, columns, this);
myTable->sortItems(1, Qt::AscendingOrder);


Everything works fine but ascendingly rows are sorted like this:
1,.........................
10,......................
2,.......................
3.......................
etc.

In other words, this sort Items function thinks that 2>10 because 10 starts with a 1. How could I avoid this and make it consider number 10 as something that's bigger than 9 and smaller than 11.
This problem may have something to do with how I load myTable with integers. This is how it's done (do you see a problem with it):



QTableWidgetItem *item = new QTableWidgetItem(QString::number(integer));
myTable->setItem(y, x, item);


Later the sorted integers are extracted from myTable like this:


QTableWidgetItem *a = myTable->item(y,x);
int val = a->text().toInt();


Thank you!

Lykurg
30th May 2009, 11:06
for that you better use
item->setData(Qt::DisplayRole, 10);
instead of setting the value on the ctor. Then your items will get sorted like integers.

codemonkey
3rd October 2009, 13:02
How do I sort a table if I use qreal as data type in one of the columns and wishes to sort on that column?