0

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.

Mohammad Kanan
  • 4,452
  • 10
  • 23
  • 47
Malkavian
  • 372
  • 1
  • 5
  • 16

1 Answers1

3

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.

Stephen Chu
  • 12,724
  • 1
  • 35
  • 46
  • 1
    Thank you for your answer!, Yes, that was a typo, When I use QDirIterator on Windows I always get the pictures in sorted manner. I was using entryList before but it's very slow compared to QDirIterator is there a fast way to iterate through directories? – Malkavian Oct 19 '12 at 01:11