PDA

View Full Version : Sorting data in QTable



sosanjay
26th October 2009, 15:08
I want to sort the column data in the table but the data is in string format and I want to do in Integer format.



ID = 0;

tbl_item12 = new QTableWidgetItem (QTableWidgetItem (QString::number(ID)));

ui->tableWidget->setItem( CurRow,12 ,tbl_item12 );
ui->tableWidget->sortByColumn(12,Qt::DescendingOrder);

but I get error on shorting Integer

tbl_item13 = new QTableWidgetItem (QTableWidgetItem (ID));




Can anyone help me to insert the integer value in column and sort it?

If possible send me the snippets of the code.

high_flyer
26th October 2009, 15:52
Why does it matter to you if the int is represented as string?
The sorting should give same results I think....

If you absolutely want to use integers, you will have to subclass QTableWidgetItem and use UserType.

sosanjay
27th October 2009, 04:37
If you sorting with String the result will not get like Integer.

from 1-9 it sort well but after 9 the result like this

3
2
12
11
10
1
0

If possible can you send me the snippets of the code.It would be great help for me.

high_flyer
27th October 2009, 09:21
[QUOTE][from 1-9 it sort well but after 9 the result like this/QUOTE]
One way of overcoming this is by adding '0's like that:
001
002
020
100
Then the strings will sort correct for the integers.

The other way is as I said, to subclass QTableWidgetItem.

[QUOTE][If possible can you send me the snippets of the code./QUOTE]
Snippets of what code?

squidge
27th October 2009, 12:20
another method would be to use a table model so you have access to the integer data, sort that, and then in your 'data' method, you would convert the integer data to string data to be displayed by the table.