QDir::SetSorting Doesn't work on Ubuntu

2019-06-14 05:31发布

问题:

I have a problem with QDir, I have this folder with lots of images, and I need to iterate through them but, they have to be sorted, so, I'm using setSorting(QDir::Name) however, It doesn't work on Ubuntu. When I iterate it with QDirIterator it selects pictures in given folder randomly. The weird thing is I use the same exact code on Windows (minGW or MSVC) and It works perfectly.

someClass::someClass(QDir dir) {
     m_dir = dir;
     m_directory.setSorting(QDir::Name);
     QStringList filter;
     filter << QString("*.") + format << QString("*.") + "jpg";
     m_dir.setNameFilters(filter);
}
someClass::iterateDir() {
     QDirIterator it(m_dir);
     while(it.hasNext()) {
         it.next();
         qDebug() << it.fileName();

         //analayze the picture here
     }
}

here it.fileName() should print (0.jpeg, 1.jpeg .... 3000.jpeg) but instead it prints (2342.jpg, 1286.jpg, 684.jpg ... 712.jpg) I tried to use other sortFlags (QDir::Size, QDir::LocaleAware) but none of them works on Ubuntu. Is there something I'm missing? Thank you for your time.

回答1:

You are mixing 2 variables: m_dir and m_directory. I assume it's typo and you mean to use the same variable.

QDirIterator does not support sorting. QDir::setSorting() only affects the list returned by QDIr:: entryInfoList() and QDir:: entryList(). Use either of them for sorted iteration.