2

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?

cppguy
  • 3,611
  • 2
  • 21
  • 36

1 Answers1

1

What you want is:

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

That way you avoid the need for a regexp.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • Perhaps I misunderstood the docs but they state that QDir::path "Returns the path. This may contain symbolic links, but never contains redundant ".", ".." or multiple separators." Also, doesn't cannonicalPath do the wrong thing on relative paths? – cppguy Oct 14 '13 at 20:49
  • So does that mean that Qt doesn't have a standard way to do this? – cppguy Oct 14 '13 at 20:50
  • No, Qt does not have a standard way to do this, because most applications don't care what goes into a path. – Kuba hasn't forgotten Monica Oct 14 '13 at 20:51