2

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

László Papp
  • 51,870
  • 39
  • 111
  • 135
adviner
  • 3,295
  • 10
  • 35
  • 64
  • As Kuba Ober says, the working directory can be anything, don’t use it in a GUI application. If you want the path where the binary is located, use http://qt-project.org/doc/qt-5/qcoreapplication.html#applicationDirPath – Frank Osterfeld May 17 '14 at 07:27

2 Answers2

1

The current directory can be anything, it solely depends on how your process is launched. What you've shown so far is that Qt Creator and Finder start the process with different current directory, that's all.

The only use for currentPath without setting it first, that I can think of, is in command line / console applications. Why do you think you need to use it? To what end?

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
1

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.

Community
  • 1
  • 1
László Papp
  • 51,870
  • 39
  • 111
  • 135