官方手册:
[static] QChar QDir::separator()
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().
See also listSeparator().
[static] QString QDir::toNativeSeparators(const QString &pathName)
Returns pathName with the '/' separators converted to separators that are appropriate for the underlying operating system.
On Windows, toNativeSeparators("c:/winnt/system32") returns "c:\winnt\system32".
The returned string may be the same as the argument on some operating systems, for example on Unix.
This function was introduced in Qt 4.2.
See also fromNativeSeparators() and separator().
[static] QChar QDir::listSeparator()
Returns the native path list separator: ':' under Unix and ';' under Windows.
This function was introduced in Qt 5.6.
See also separator().
使用 QDir::separator() 的好处:
- 在表示路径的代码中,允许存在空格;
- 在跨平台应用场景,不需要自己考虑路径中的 “/” 或 "\" 问题;
- 在win中,C:/abc/def\ghi # 同时存在 “/” 和 "\",路径有效;
- 在linux中,“/”有效, 或 "//"有效。
- 在linux中表示 win路径, "\\"有效,第一个 "\" 表示转移字符。
结论:"/" 在上述 Linux 和 win平台中通用;其他平台未测试。