c++ - Mac 上的 QDir 绝对路径

标签 c++ macos qt qtcore qdir

当我在 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/

相关文章:

c++ - C++中的指针和运行时错误

c++ - 在 C++0x 中传递/移动构造函数的参数

java - 从 macOS 系统设置中获取日期和时间格式

linux - Qt 5.5 未绑定(bind)到端口

c++ - 64位系统上Visual Studio 2012 32位编译

mysql - 无法在 MAC 上停止 MySQL

java - 为什么 command/usr/libexec/java_home 不起作用?

linux - 在 Windows 上运行为 Linux 构建的 Qt 应用程序

C++ Qt 多重定义

c++ - 为什么 std::shared_ptr<void> 起作用