Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to decompose a QDir into a QStringList

Tags:

c++

qt

qstring

qdir

I am trying to turn a QDir which can contain relative or absolute paths into a list of folders in a QStringList like this:

const QString path = dir.path ();
return path.split (QRegExp ("[\\/]+"), QString::SkipEmptyParts);

This ideally would turn a path like C:\foo\bar into a list of strings "C:", "foo", and "bar"

Is there a better way to do this that is already implemented in Qt?

like image 995
cppguy Avatar asked Feb 03 '26 06:02

cppguy


1 Answers

What you want is:

QDir::toNativeSeparators(dir.path()).split(QDir::separator(), 
                         QString::SkipEmptyParts);

That way you avoid the need for a regexp.

like image 79
Kuba hasn't forgotten Monica Avatar answered Feb 04 '26 21:02

Kuba hasn't forgotten Monica