Sorting a QListWidget using sortItems()

前提是你 提交于 2020-05-15 10:04:58

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/8493671/sorting-a-qlistwidget-using-sortitems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!