Current working directory
From QtCentreWiki
Problem
An unexpected working directory might sometimes cause problems while using relative paths to point to external resources. Still, there are situations when you either can't or don't want to use absolute paths or embed resources into the application binary. And just to give out a real example of a situation like that, QSound does not even support resources.
For example launching an application by clicking on the executable in Konqueror causes the working directory to be set as user's home directory. Also, some Windows compilers which tend to place application binaries under Release and Debug subfolders are more or less troublemakers.
Solution
You may use the following code to force the current working directory to the same as the directory containing application binary with the exception of those subfolders mentioned in the previous section.
QDir dir(QCoreApplication::applicationDirPath());
#if defined(Q_OS_WIN)
if (dir.dirName().toLower() == "debug" ||
dir.dirName().toLower() == "release")
{
dir.cdUp();
}
#elif defined(Q_OS_MAC)
if (dir.dirName() == "MacOS")
{
dir.cdUp();
dir.cdUp();
dir.cdUp();
}
#endif
QDir::setCurrent(dir.absolutePath());
The above code snippet shall be placed anywhere in your application after instantiating the application object. This could be for example in main() or in constructor of Q(Core)Application subclass.
jpn 14:35, 13 December 2006 (CET)

