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

て烟熏妆下的殇ゞ 提交于 2019-11-27 06:42:08

问题


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?


回答1:


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).




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/3194725/cross-platform-way-of-constructing-an-fs-path-with-qt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!