Cross-platform way of constructing an FS path with Qt [duplicate]
Asked Answered
I

3

29

Possible Duplicate:
Qt equivalent of PathAppend?

Short story: does Qt 4 have an analog of Python's os.path.join?

Long story: I need to add a relative path to the application directory, QCoreApplication::applicationDirPath() in the Right Way (TM), so that the code doesn't depend on the file system directory separator character.

Is merely joining QStrings and using "/" as the separator a good solution?

Impresario answered 7/7, 2010 at 12:41 Comment(4)
Why not using QDir or similar?Caul
Because I do not see a QDir method that does exactly what I need. Could you please be more specific about what you suggest?Impresario
Operator overloading can be abused, but overloading operator/ for paths is not. QCoreApplication::applicationDirPath() / "MySubDir" is perfectly reasonable code. Shame Qt4 doesn't have that.Conti
the method QChar separator() will give you at least the separator you need to useCaul
K
24

You can either use "/" directly or use QDir::separator(). But in general use a QDir for this (which translates "/" to the platform specific path separator for you).

Kent answered 7/7, 2010 at 12:51 Comment(0)
K
19

From Qt 4.6 QDir documentation,

Qt uses "/" as a universal directory separator in the same way that "/" is used as a path separator in URLs. If you always use "/" as a directory separator, Qt will translate your paths to conform to the underlying operating system.

So, I guess QDir will be helpful for you.

Kampmeier answered 7/7, 2010 at 13:2 Comment(1)
+1 for docs. Anyways, / is a pretty universal seperator. It's default on POSIX, Mac OS, and supported (officially) by Windows.Hillari
F
9

Just use "/" when staying in the Qt world. To convert the path for non-Qt classes and functions etc., use QDir::toNativeSeparators( path ).

Freewheel answered 7/7, 2010 at 14:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.