当我在 Qt Creator 中运行相同的构建以及当我从 Mac 上的 Finder 中双击它时,我得到两条不同的路径。
这是我的代码:
QDir dir = QDir::currentPath();
dir.cdUp();
dir.cdUp();
dir.cdUp();
QString rootPath = dir.absolutePath();
当我在 Qt Creator 中运行(调试)模式时,我的路径是:
/Users/myuser/Projects/AppName/build/mac
当我双击位于的文件时 /Users/myyser/Projects/AppName/build/mac 从查找器返回 /仅。
为什么我会得到两条不同的路径?
版本:Qt5.2.1
更新
阅读以下 URLhttp://qt-project.org/forums/viewthread/34019 似乎是一个错误
最佳答案
Why would I get two different paths?
正如他们在您链接的线程中写入的那样, QDir::currentPath() 不一定返回应用程序目录。它将返回应用程序运行位置的路径,该路径不同于从命令行运行应用程序时的应用程序目录,甚至不同于“开始菜单”类似位置等。
如果您希望处理应用程序目录以从那里导航,则需要使用以下方法:
QString QCoreApplication::applicationDirPath() [static]
Returns the directory that contains the application executable.
For example, if you have installed Qt in the C:\Qt directory, and you run the regexp example, this function will return "C:/Qt/examples/tools/regexp".
On Mac OS X this will point to the directory actually containing the executable, which may be inside of an application bundle (if the application is bundled).
最后一句话甚至澄清了 Mac OS X 的情况。
关于c++ - Mac 上的 QDir 绝对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23707866/