tags:

views:

705

answers:

3

hi,

Is there a class that handles file paths in Qt? Particularly I'm looking for something like .NET's Path.Combine.

I know there's one in boost::filesystem but I was wondering if there's one in Qt.

+9  A: 

There is QDir which might be of help (see QDir::relativeFilePath and QDir::canonicalPath and others).

Quoting from QDir doc:

A QDir is used to manipulate path names, access information regarding paths and files, and manipulate the underlying file system.

MartinStettner
Actually QDir is relatively stupid class. For example, it doesn't allow to cd() to nonexisting dir.
Sergei Stolyarov
this a joke right ? No program can cd into non existing dir. QDir and QFile are very flexible, I highly recommend it.
Bluebird75
What Sergei meant was `QDir::cd()` which is (should be) just a shortcut for `return QDir( this->filePath( arg ) )`. This has nothing to do with changing the CWD of the process. If `QDir::cd()` would allow non-existing names, then that would make it _the_ Qt class for abstracting away a Path (currently, `QString` serves that purpose - not!). Like it is now, if Sergei is right, it fails to meet up to the potential, which _indeed_ is a sad design mistake.
+1  A: 

QDir provides access to directory contents and allows various manipulation (mkdir for example). But there are no classes for path components manipulation unfortunately.

Sergei Stolyarov
+3  A: 

Another class that might be useful is QFileInfo.

From Qt documentation:

The QFileInfo class provides system-independent file information.

QFileInfo provides information about a file's name and position (path) in the file system, its access rights and whether it is a directory or symbolic link, etc. The file's size and last modified/read times are also available. QFileInfo can also be used to obtain information about a Qt resource.

swongu