我正在使用以下内容阅读目录内容QDir::entryList()
。其中的文件名结构如下:
index_randomNumber.png
我需要将它们排序index
,Windows 资源管理器对文件进行排序的方式,以便我得到
0_0815.png
1_4711.png
2_2063.png
...
而不是排序依据QDir::Name
给我:
0_0815.png
10000_6661.png
10001_7401.png
...
Qt 中是否有内置方法可以实现此目的,如果没有,那么实现它的正确位置是什么?
如果你想使用QCollator https://doc.qt.io/qt-5/qcollator.html对返回的条目列表中的条目进行排序QDir::entryList https://doc.qt.io/qt-5/qdir.html#entryList,您可以对结果进行排序std::sort() http://en.cppreference.com/w/cpp/algorithm/sort:
dir.setFilter(QDir::Files | QDir::NoSymLinks);
dir.setSorting(QDir::NoSort); // will sort manually with std::sort
auto entryList = dir.entryList();
QCollator collator;
collator.setNumericMode(true);
std::sort(
entryList.begin(),
entryList.end(),
[&](const QString &file1, const QString &file2)
{
return collator.compare(file1, file2) < 0;
});
根据獾 https://stackoverflow.com/users/991000/the-badger的评论,QCollator
也可以直接用作参数std::sort
,替换 lambda,因此调用std::sort
变成:
std::sort(entryList.begin(), entryList.end(), collator);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)