Can Qt build "paths" ?
-
Hey
I'm in need of some "path building" that is correct depending on platform.
Would qt be able to provide it ? something similar to python Path().joinpath("this", "is", "my", "fancy", "path");
?
-
Check QDir out
QStringList nameList;
// add names to nameList
QDir dir(nameList.join(QDir::separator())); /* no worries*/ -
Internally Qt uses
/
as path separator, but all path taking APIs accept both/
and\
, so you can just always use/
(which is easier because you don't have to escape it).
If you need to pass the path to some native platform API or display it to the user in a platform correct form you can use QDir::toNativeSeparators, which will convert all the separators to current platform appropriate.
It's usually not needed, but if you insist on building the platform correct path yourself you can use QDir::separator().So both of those will do the same:
QString path1 = QDir::toNativeSeparators("\\this/is\\my/fancy\\path"); QChar sep = QDir::separator(); QString path2 = sep + "this" + sep + "is" + sep + "my" + sep + "fancy" + sep + "path".
1/4