PDA

View Full Version : QTableView + sorting + numbers & special characters



foggy-mind
2nd June 2009, 11:40
Hi all,
For the first - sorry for my english!!!...;)

OS: WinXP
QT: 4.5.1 (opensource)
Compiler: MinGW

Please help me with sorting numbers & special characters in QTableView.

set data to QStandardItemModel

itemModel = new QStandardItemModel;
int row = 0;
while (query->next())
{
item = new QStandardItem;
itemModel->setItem(row, 0, item);
itemModel->setData(itemModel->index(row,0), Qt::Unchecked, Qt::CheckStateRole);
item = new QStandardItem(tr("%0").arg(query->record().value("ltr_cust_code").toString()));
itemModel->setItem(row, 1, item);
item = new QStandardItem(tr("%0").arg(query->record().value("kk").toString()));
itemModel->setItem(row, 2, item);
//etc........
row++;
}

set QStandardItemModel to QTableView


ui->tlvXMLList2->setModel(xmlModel2);
ui->tlvXMLList2->setSortingEnabled(true);
ui->tlvXMLList2->sortByColumn(1, Qt::AscendingOrder);


First problem:
The 2'nd column has data which is int type, and sorting is something like that [1,10,11,...,2,21,...] I need that numbers will be sorted [1,2,...,10,11,...,21,...]

Second problem:
In 3'rd column data is text type and has lithuanian characters (ĄČĘĖĮÅ ŲŪŽ) (if you don't see the characters set character encoding on your browser Baltic (Windows-1257)). When I'm sorting data using this column then data sorts something like that [A,B,C,D,E,F,G,...,ĄČĘĖĮ,...]. In lithuanian language sorting of text with lithuanian characters is [A,Ą,B,C,Č,D,E,Ę,Ė,F,G,...]
How do everything what I need ??? :confused:

Thank's

Lykurg
2nd June 2009, 14:21
First problem:
The 2'nd column has data which is int type, and sorting is something like that [1,10,11,...,2,21,...] I need that numbers will be sorted [1,2,...,10,11,...,21,...]

use


item = new QStandardItem;
item->setValue(query->record().value("ltr_cust_code").toInt());
itemModel->setItem(row, 1, item);


if all your entries are integers...

Lykurg
2nd June 2009, 14:25
Second problem:
In 3'rd column data is text type and has lithuanian characters (ĄČĘĖĮÅ ŲŪŽ) (if you don't see the characters set character encoding on your browser Baltic (Windows-1257)). When I'm sorting data using this column then data sorts something like that [A,B,C,D,E,F,G,...,ĄČĘĖĮ,...]. In lithuanian language sorting of text with lithuanian characters is [A,Ą,B,C,Č,D,E,Ę,Ė,F,G,...]

Subclass your model and alter QAbstractItemModel::sort();

and


item = new QStandardItem(tr("%0").arg(query->record().value("kk").toString()));

could be easily written as


item = new QStandardItem(query->record().value("kk").toString());

foggy-mind
3rd June 2009, 07:28
use


item = new QStandardItem;
item->setValue(query->record().value("ltr_cust_code").toInt());
itemModel->setItem(row, 1, item);


if all your entries are integers...

Yes all entries is integers... But QStandardItem don't have setValue function.....



Subclass your model....


you talking about this manual??? Model Subclassing Reference (http://doc.trolltech.com/4.5/model-view-model-subclassing.html)

Lykurg
3rd June 2009, 16:32
Yes all entries is integers... But QStandardItem don't have setValue function.....

Ah, right, confused :eek: -> setData()!


you talking about this manual??? Model Subclassing Reference (http://doc.trolltech.com/4.5/model-view-model-subclassing.html)

Yeah, why not. But is much more easier if you subclass QStandardItemModel, because then you just have to override the sort function


void MyModel::sort( int column, Qt::SortOrder order)
{
if ("yourcolumn" == column)
// do custom sorting
else
QStandardItemModel::sort(column, order);
}



EDIT: Just look how QStandardItemModel implements that function and do it the "same" way.

foggy-mind
4th June 2009, 11:11
Ok I'll try.... thanks