PDA

View Full Version : How to sort QListWidget by numbers...



Patrick Sorcery
26th September 2010, 04:07
So here I created a QListWidget, put some QListWidgetItems in it, and turned the sorting on by QListWidget::setSortingEnabled(true). Then I got such a result: 1, 10, 2, 3(sort by string)... while I had expected 1, 2, 3, ..., 10 (sort by number)

& then I searched for information through Google but only got methods of Qt3. So how to solve this problem in Qt 4.6.2?

Thanx!

tbscope
26th September 2010, 06:54
Very easy, just reimplement the < operator in your custom QListWidgetItem

Lykurg
26th September 2010, 07:57
Use QListWidgetItem::setData() not the constructor to set your value. Then all will work like you expect it to work.

EDIT: since we are in Newbie I am gentile:
int yourIntValue = 123456;
QListWidgetItem *item = new QListWidgetItem;
item->setData(Qt::DisplayRole, yourIntValue);

Goug
7th December 2011, 01:53
I have a similar but slightly more complex variation. I have the numbers 1 through 16, prefixed with the letter "P", i.e. P1 - P16. As far as I can see, there's no way to have the sort use the numeric value, but have the display use the corresponding string. Is that correct?

Thank you,
Doug

Santosh Reddy
7th December 2011, 03:04
Yes, as long as QListWidget is used.

norobro
7th December 2011, 03:05
As tbscope says above, reimplement the < operator and use QString::rightJustified() (http://doc.qt.nokia.com/latest/qstring.html#rightJustified) in your comparison statement.

shivraj100raghu@gmail.com
6th November 2017, 13:30
Hello Guys,

I am new in QT, Can you please provide me some solution to sort the integer as a string by QList or QTableView .

I am using QTableView and QStandardItemModel, In this, I am passing the integer value but it converts in string like: QStandardItem *trackIDItem = new QStandardItem(QString::number(trackID));
And As we know TableView provides by default true option for sorting but It does not do in ascending or descending order same as QList<QString> list; list<<"1"<<"2"<<"4"<<"50"<<"30"; qSort(list);
Output should be : 1, 2, 4, 30, 50 but It does not give as expected same problem with QTableView.

Please provide me some solution to sort out this problem.

Thanks in Advance.

d_stranz
6th November 2017, 18:39
There are at least two possible solutions:

1 - Derive a new class from QStandardItemModel and reimplement the data() method. Add a custom Qt::ItemDataRole (for example, use Qt::UserRole) and add a clause to handle this role to the data() method. When data() is called with any role except Qt:: UserRole, return the value from the base class QStandardItemModel::data() method. For Qt:: UserRole, return the *integer* value of the display string. Very important: When you set up your initial model, call QStandardItemModel::setSortRole() using Qt:: UserRole (or the role you chose) as the argument. For this solution, you can use QStandardItem as the items added to the model without changes.

2 - Derive a new class from QStandardItem and reimplement the QStandardItem::operator<() method. In this method, compare the *integer* values of your display strings and return the appropriate Boolean result. When you add items to your model, you will create instances of this derived class instead of QStandardItem. For this method, you can use QStandardItemModel without changes.