Solved How to use QDir(); to get a path " with native separators"
-
@Cobra91151
It depends where/why you use it. I think the docs is pretty clear:Returns the native directory separator:
"/"
under Unix and"\"
under Windows.You do not need to use this function to build file paths. If you always use
"/"
, Qt will translate your paths to conform to the underlying operating system. If you want to display paths to the user using their operating system's separator use toNativeSeparators().While you manipulate paths within Qt you would be best using
/
, regardless of platform. So you don't need this. Only when you come to, say, passing a Qt-style path to an operating system command or displaying a message do you need to useQDir::separator()
ortoNativeSeparators()
.I happenstanced across https://agateau.com/2015/qdir-separator-considered-harmful/, that is short, clear (big writing, friendly font ;-) ) and illustrates the trap.
-
Because a day - here in the forum - i got this informations:
"Qt use "/" as path separator for all OS.
QDir::separator will only be used in QDir::toNativeSeparators.""While you are using Qt path functions you should always use /, and that is what they will return to you. If you want the native separators QDir has to/fromNativeSeparartors(), but these should only be used if passing to something external, like an OS command."
Hence for me it meant - never use "QDir::separator" it is Qt internal only ....
What I know get is better to use something like this example:
QString mypath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) + QDir::separator() + "my folder";
Thank you very much for making it more clear.
-
@Christian-Ehrlicher said
This is wrong for the arguments you pass to the process - Qt will not modify them in any way.
But it will add "spaces" between all argument "peaces".
Wich leads into this to be correct:
arguments << "-opiton1" << "-ringthebell" << "yes" ... ; -
@ademmler said in How to use QDir(); to get a path " with native separators":
QString mypath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) + QDir::separator() + "my folder";
For anyone coming across this: As per the article referenced earlier, this is precisely the wrong place to use
QDir::separator()
. -
@JonB thx for responding.
Maybe I am mistaken - but from the discussion above I got the recommendations to use QDir::separator(). Now I am not sure which answer is the correct one.Pls see above first comment from @Christian-Ehrlicher
-
I have checked it on
Windows
, this path will workC:/test/path\test.exe
even thought it is wrong. So, this codeQString myPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) + QDir::separator() + "my folder";
in your case:QStandardPaths::writableLocation(QStandardPaths::DesktopLocation)
will have"/"
and
QDir::separator() + "my folder"
will have "\my folder".It will work, but the path is not correct. Instead, there are a lot of options we can do here.
QString myPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation).replace("/", "\\") + "\\" + "my folder";
QString myPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation).replace("/", "\\") + "\\my folder";
Or
QString myPath = QDir::toNativeSeparators(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation).append("/") + "my folder");
QString myPath = QDir::toNativeSeparators(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation).append("\\") + "my folder");
QString myPath = QDir::toNativeSeparators(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) + "/" + "my folder");
QString myPath = QDir::toNativeSeparators(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) + "\\" + "my folder");
QString myPath = QDir::toNativeSeparators(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) + QDir::separator() + "my folder");
The path should contain all "/" or "\" to be correct.
All, these paths will return:"C:\\Users\\username\\Desktop\\my folder"
and it is valid and correct forWindows
, so you can use it to build paths and display to the user. TheQDir::toNativeSeparators()
method will display the correct "/" or "\" depending on the currentOS
, and will fix the path issue withQDir::separator()
. -
@Cobra91151
That's a long answer! :)Yes, the issue will show up under Windows only.
C:/test/path\test.exe
is the kind of thing that will result from usingQDir::separator()
. Which looks wrong, and may (or may not) cause problems.The point is to just use
/
while passing a path around in Qt code. SoQString mypath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) + "/my folder";
would be best.
You only need
QDir::separator()
ortoNativeSeparators()
when you're e.g. passing to OS or printing. -
@ademmler said in How to use QDir(); to get a path " with native separators":
Maybe I am mistaken - but from the discussion above I got the recommendations to use QDir::separator(). Now I am not sure which answer is the correct one.
Pls see above first comment from @Christian-Ehrlicher
@Christian-Ehrlicher only said that you should use native separators when writing the path to an external non-Qt process.
In your example, you are not writing the path to an external process. You are building up a path inside Qt. So, QDir::separator() is not appropriate here.
The correct way to build your path in a cross-platform way is
QString mypath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) + "/my folder";
like @JonB said. -
@Christian-Ehrlicher @Cobra91151 @JonB @KroMignon and all the others:
This had been a very longish task - but I am glad about it - because it demystified some obstacles for me.
Somehow QProcess(); and QStandardPaths(); got mixed up - but that was very good for some reason.Thx again - which answer shall I mark as the solution now? You ALL have been very helpful.
Another question came up into my mind. Whats about using QFile::copy(); function.
Does this take care of platform depended details (in path names) automatically ?QString targetFile(myFolder.absolutePath() + "/" + base + "." + ext); QFile sourceFile(inputFile); bool ok = sourceFile.copy(targetFile);
-
Hi @ademmler,
Another question came up into my mind. Whats about using QFile::copy(); function.
Does this take care of platform depended details (in path names) automatically ?Every Qt API takes forward slashes as separator. You only need native separators when you display a path to the user or when you pass it to some thirdparty API. (Side node: even the Windows API allows forward slashes in many cases).
So yes,
QFile::copy
takes care for the platform details itself.Regards
-
@aha_1980 thank you for proving this