On 10/30/2013 8:59 AM, Konrad Rosenbaum wrote:

Hi,

On Wednesday 30 October 2013 00:58:05 Calogero Mauceri wrote:

> The Qt documentation states that QDir::currentPath() returns "The

> application working directory". Shouldn't the workind directory be

> initialized with the path the application was launched from? If that's

> not the case, which is the right way to obtain the working directory?

> (not the executable path as the QApplication::applicationDirPath() is

> returning)

The current working directory is the directory the application is currently working in (I know, it sounds like a tautology). Or in other words: if you open a file with a relative path name, the current working dir is the one in which your program looks for the file:

QFile file("myfile");

file.open(QIODevice::ReadOnly);

is equivalent to:

QFile file(QDir::current()+"/myfile");

file.open(QIODevice::ReadOnly);

You can change it with QDir::setCurrent or the underlying OS function chdir.

At application startup the current working directory is the exact same directory that the parent was in when it spawned your process (this is somewhat simplified). This is the same behavior over all desktop OSes. The parent application is free to chose: it can simply refuse to care (result: your CWD is more or less random from your programs perspective; this is why an app started from a KDE desktop is almost always in $HOME, but sometimes somewhere else); it can assume you are a command line tool and want to work in the same place the shell is in (that's what happens if you start anything from a shell); it can try to be "helpful" and switch to the directory the binary is in (apparently what Mac used to do); or it can try to make things consistent by switching to the root directory (apparently what 10.9 does).

In short: relying on the CWD at startup is misguided for most GUI apps. If you need to be in a specific directory: use some other mechanism to find it and then switch there, do not rely on being placed there by magic.

Places you may want to be:

* somewhere relative to your binary: use QApplication::applicationDirPath()

- I do this in apps that have complex trees of files with data needed at

runtime, apps that work in their own little "universe"

* in the users home directory: QDir::setCurrent(QDir::homePath())

- desktop apps that interact with user files

* in the exact same place you were started from: don't move

- this is true for most command line tools (like qmake, moc, ...)

* in the root directory

- true for most server processes (daemons, middleware, ...)

* in the temp dir: use QDir::setCurrent(QDir::tempPath())

- e.g. simulations that create some throw-away data only

* in some dir that was configured by the user: read the config then switch

- true for other server-like processes (e.g. automation software, ...)

The tricky question we're trying to coax out of you: does QDir.current() really return an empty string "" or the root directory "/"? The former may indicate a bug on Apple's part, the latter is a perfectly valid place to be in.


Konrad, thanks for your exaustive explanation.
It is clearer to me now that I can not rely on QDir::currentPath() function for what I need.

QDir::currentPath() on Mac OS X 10.9 is returning the root path "/".

Thanks,
    Calogero

_______________________________________________
Development mailing list
Development@qt-project.org
http://lists.qt-project.org/mailman/listinfo/development

Reply via email to