3

My application is using QDirIterator to iterate through .jpg images in a folder. The are named page0, page1, page2 ... page10, page 11... and so on.

The problem is that it is searching the files in the order page0, page1, page10, page11 and so on. How can i change this? Is there a option for ordering the files correctly?

Here is a small part of my code:

QDirIterator it(directory, QDirIterator::Subdirectories);        
while (it.hasNext()) {

        qDebug() << it.filePath();


        if (it.filePath().contains(".jpg"))
        {
            string ImagePath = it.filePath().toStdString();

            Mat img= cv::imread(ImagePath,3);

            vectorMatchQuality.push_back(BestMatch(img, templ));

            vectPath.push_back(ImagePath);

        }

        it.next();
}

I'm am new to C++ and Qt. Any help, tip or answer is appreciated :)

sashoalm
  • 75,001
  • 122
  • 434
  • 781
Ole Henrik Skogstrøm
  • 6,353
  • 10
  • 57
  • 89
  • This might not be your main issue here, but `it.next();` should be at the top of the while loop. – philipp Dec 12 '17 at 13:40

2 Answers2

8

QDirIterator Class Reference

Unlike QDir::entryList(), QDirIterator does not support sorting.

You cannot change the order of elements in a QDirIterator. But QDir::entryList() won't help you either, since it supports only a specific set of sort oders.

It's much better to rename your files. Think about this way:

Image0
Image1
Image2
Image3
   ...
Image9
Image10

A usual string sorting algorithm does a lexical comparison between two strings, which works like

  1. Take the next symbol
  2. If there is no next symbol go to 5
  3. If the symbols differ then
    • If the first symbol is lesser than the second return FIRST_LESS
    • else return FIRST_MORE
  4. If they don't differ goto 1
  5. If the first string still has symbols return FIRST_MORE
  6. If the second string still has symbols return FIRST_LESS
  7. Return SAME

And this is why you'll get all files starting with a zero first, then all files starting with a 1 and so on:

Image0
Image1
Image10 ; because 1 < 2
Image11 ; because 1 < 2
Image12 ; because 1 < 2
 ...
Image2
Image20 ; because 2 < 3
Image21 ; because 2 < 3
Image22 ; because 2 < 3
 ...

The easiest way to fix this is to change the file names. Otherwise you have to write your own file name parser, which takes the file name, splits it into name + number and sorts according to the number.

codeling
  • 11,056
  • 4
  • 42
  • 71
Zeta
  • 103,620
  • 13
  • 194
  • 236
  • Nice informative answer, thank you. So that leaves me with a different question. How do i change the comon naming scheme? i will open another question for this. Thank you – Ole Henrik Skogstrøm Nov 11 '12 at 14:17
  • Thanks for the tipe on QDir it helped me a lot for my issue, QDirIterator founded files unusally wrong. – adi Oct 14 '14 at 16:19
8

I know this is an old question but an updated answer for Qt 5.13 could be to use the QDir::SortFlag with your QDir object

Example use is:

QDir dir(pathToDirectory);
dir.setSorting(QDir::SortFlag::DirsFirst | QDir::SortFlag::Name);

This sorts directories first, then all dirs/files by name;

Hope it helps.

CybeX
  • 2,060
  • 3
  • 48
  • 115