Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting a QListWidget using sortItems()

I have issues while using sortItems() on a QListWidget. Here is what happens:

Changes this:

A z d C E o I

to this:

A C I d e o z

But i want it to be:

A C d e I o z

Any way to change to make it sort like that?

like image 636
Kazuma Avatar asked May 07 '26 12:05

Kazuma


1 Answers

QListWidget uses the DisplayRole (the item's text) for sorting and calls QString::localeAwareCompare on them. To override the default, you can either change the locale used (see QLocale) or have your own QListWidgetItem subclass reimplementing QListWidgetItem::operator< and let it do the comparison you want.

For anything more advanced, I'd suggest to have a look at QListView, which you could combine either with your own model implementation or the convenience models QStandardItemModel or QStringListModel (the latter could be the most simple and elegant solution in your case). For sorting, you would then put a QSortFilterProxyModel in between the actual model and the view.

like image 151
Frank Osterfeld Avatar answered May 09 '26 10:05

Frank Osterfeld



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!