PDA

View Full Version : QDir::entryList() get absolute path



Aji Enrico
21st April 2011, 19:01
edit: (Maybe this should have been posted at the "Newbie" section)

Hi

I'm trying to get all folder names with absolute paths. My question is maybe kinda simple, does my implementation have any pitfalls?


QDir dir("R:/");
dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot).replaceInStrings(QRegExp("^"),dir.absolutePath())

QDir::entryList() only returns a QStringList containing directory names without full paths.

So my simple one line solution would be using QStringList::replaceInStrings() with
parameters QRegExp("^") and dir.absolutePath(),
which should add the string "R:/" as a prefix to each directory entry, giving me full paths.
As I said, does my solution have any pitfalls like for example if the QRegExp("^") might get applied elsewhere in the string instead of only in the beginning as a prefix?

I have tried naming folders with spaces, symbols and it seems to me that it works.
Maybe I'm just paranoid but I would love to hear about your expert opinions :). Much appreciated.

Also, would be nice if you guys and gals could share your favorite solutions.

(It's a pity IMHO that QDir::absoluteEntryList() doesn't exist)

Thanks for reading

Lykurg
21st April 2011, 21:16
What's about using QDir::entryInfoList() and then use them. If you need a QStringList with the path then iterate over the list and use e.g. QFileInfo::absolutePath().

Aji Enrico
22nd April 2011, 13:13
Yeah I thought about it first, to iterate through each QFileInfo and let that handle the absolute paths return, maybe that is safer.

htroyo
23rd April 2011, 04:26
http://doc.trolltech.com/latest/qdir.html#absoluteFilePath
maybe it could be useful, i hope it works for you