0

I tried to split a QString (a filename) and I want to get parts between two dash signs in the filname.

The Filname is for example "0000000398_05WA-1384864213-218.bmp" .

However,

QStringList query;
QString filename;
QDirIterator it(qDirPictures, QDirIterator::NoIteratorFlags);

while (it.hasNext()) {

    it.next();
    filename = it.fileName();

    query = filename.split("-");

    qDebug()<<query;
}

gives me a correct output:

("0000000398_05WA", "1384864213", "218.bmp")

But if i want to access the second list item in the same iteration with:

qDebug()<<query.at(1);

I get an

"ASSERT failure in QList::at: "index out of range"...

However, if i try with:

qDebug()<<query.at(0);

I get the correct output:

"0000000398_05WA"

Whats wrong ?

refuzee
  • 408
  • 4
  • 15

2 Answers2

2

you could also use section

QDebug() << filename.section("_",1,1); // will print "1384864213"
ratchet freak
  • 47,288
  • 5
  • 68
  • 106
  • As i need both numbers, i decided to go with: ` part = filename.section("-",1,1); filename = filename.section("-",2,2); parttwo = filename.section(".",0,0);` - thanks! – refuzee Nov 25 '13 at 15:23
0

Just for solving the original mehtod:

If somebody wants to go with QString.split in this case:

adding

qDirPictures.setFilter(QDir::Files);

before the while loop makes it work well with QString.split with the original code. Turns out that without any filters the first two output lines or directory list items are:

"." 

and

".." 

which leads to the out of bounds error.

refuzee
  • 408
  • 4
  • 15