Question

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 ?

Was it helpful?

Solution

you could also use section

QDebug() << filename.section("_",1,1); // will print "1384864213"

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top