How to sort QListWidget by numbers...
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!
Re: How to sort QListWidget by numbers...
Very easy, just reimplement the < operator in your custom QListWidgetItem
Re: How to sort QListWidget by numbers...
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:
Code:
int yourIntValue = 123456;
item->setData(Qt::DisplayRole, yourIntValue);
Re: How to sort QListWidget by numbers...
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
Re: How to sort QListWidget by numbers...
Yes, as long as QListWidget is used.
Re: How to sort QListWidget by numbers...
As tbscope says above, reimplement the < operator and use QString::rightJustified() in your comparison statement.
How to sort the number as a string by QList and QTableView
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.
Re: How to sort the number as a string by QList and QTableView
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.