PDA

View Full Version : QTableWidget Sorting



mclark
29th September 2006, 20:55
I would like to sort the rows of a QTableWidget. The cell I want to sort on contains an icon indicating either an online or offline device (along with accompanying tooltip). I don't want to put visible text into that cell (I want the cell to have a small width) but would like to sort the rows based upon online status.

First question, can there be hidden text in a cell?
If so, can a sort be accomplished using hidden text?

C:\Documents and Settings\mclark\My Documents\My Pictures\online.jpg

wysota
29th September 2006, 21:49
First question, can there be hidden text in a cell?
Sure. You can use a custom role (different than Qt::DisplayRole) to store the text.

If so, can a sort be accomplished using hidden text?
Yes, but you can sort according to an image too. You have to reimplement QTableWidgetItem::operator<() to return the proper order of items. As you probably want to sort according to sort order selected in the table widget, you'll have to access the table through QTableWidgetItem::tableWidget() and get to the proper column by accessing appropriate header and its sortIndicatorSection. You can then compare pixmaps somehow (you can't do it directly as QPixmap doesn't have equality operator defined).

mclark
29th September 2006, 21:52
Thanks for the quick reply. I'll give your suggestions a try.

mclark
29th September 2006, 23:25
I created the QTableWidgetItem using the Qt::UserRole type.
setItem(nRow, COL_Status, new QTableWidgetItem("Offline", Qt::UserRole));The text always appears. Did I read your response incorrectly or is there more to this?

wysota
29th September 2006, 23:34
Did I read your response incorrectly
Yes, read it again and read the docs again - you're using Qt::DisplayRole because of incorrect use of the constructor (the second parameter doesn't determine the role but the type of item). It won't sort according to UserRole anyway... You'll still have to subclass and reimplement the operator and if you do, you don't need the additional role (and even if you don't want to sort images, you'll probably want to use the ToolTipRole anyway).