PDA

View Full Version : QTableWidget - setSortingEnabled



bruccutler
25th April 2007, 14:35
Hello,
I have enabled sorting using the setSortingEnabled call on a QTablewidget.

How do I control how it sorts. For example, I have a column with number that I want sorted numerically, not alphabetically, how do I do that? I also want to sort case-insensitive, rather than case sensitive, how do I do that?

- BRC

jpn
25th April 2007, 14:46
For numerical sortiing, set the data as integers, not as text:


QTableWidgetItem* item = ...
// item->setText("2"); // <-- not like this
item->setData(Qt::DisplayRole, 2); // but this


For case insensitive sorting, you might need to reimplement QTableWidgetItem::operator<() (http://doc.trolltech.com/4.2/qtablewidgetitem.html#operator-lt).

bruccutler
25th April 2007, 15:13
Thanks for the reply.

"Roles" are a bit confusing. Where's a good reference on what Qt means by "roles"?

Again, thanks for the help.
- BRC

jpn
25th April 2007, 15:27
"Roles" are a bit confusing. Where's a good reference on what Qt means by "roles"?

Item Roles (http://doc.trolltech.com/4.2/model-view-model.html#item-roles)
enum Qt::ItemDataRole (http://doc.trolltech.com/4.2/qt.html#ItemDataRole-enum)