Search code examples
c++macosqtqtcoreqdir

QDir absolutePath on Mac


Im getting two different paths when i run the same build within Qt Creator and when I double click on it from the Finder on a Mac.

Here is my code:

QDir dir = QDir::currentPath();
dir.cdUp();
dir.cdUp();
dir.cdUp();
QString rootPath = dir.absolutePath(); 

When I run it (debug) mode in Qt Creator my path is:

/Users/myuser/Projects/AppName/build/mac

When I double click on the file that is located on /Users/myyser/Projects/AppName/build/mac from finder it returns / only.

Why would I get two different paths?

Version: Qt5.2.1

Update

Seems like its a bug from reading the following URLhttp://qt-project.org/forums/viewthread/34019


Solution

  • Why would I get two different paths?

    As they write in the thread you linked, QDir::currentPath() does not necessarily returns the application directory. It will return the path from wherever the application is run, which will be different than the application directory when running the application from the command line, or even from "start menu" alike places and so on.

    If you wish to deal with the application directory to navigate from there, you would need to use the following method instead:

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

    The last sentence even clarifies the Mac OS X case.