PDA

View Full Version : currentPath is started always where the executable is?



Dark_Tower
7th April 2006, 14:58
Hi, I need to know the directory where the application is stored because I have to save some configuration files in the directoy "config" placed on this directory. My question is: "QDir::currentPath" always "points" at the beginning of the app at this directory in all platforms? I think so, but I want to know it for sure ;) Thanks.

L.Marvell
7th April 2006, 15:49
Let's look what Assistant says:

QString QDir::currentPath () [static]
Returns the absolute path of the application's current directory.
It seems it does ;)

jpn
7th April 2006, 15:50
An application can be launched from a different directory where the executable itself lies.
"Current dir" is assumably the directory where the executable was launched from.
So I wouldn't suggest using that kind of approach..

Maybe you could just use QSettings instead? Or alternatively QDir::homePath()..
(Note that the latter might be problematic on windows, though.)

jpn
7th April 2006, 15:58
Let the example speak for itself:


#include <QCoreApplication>
#include <QDir>
#include <QtDebug>

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug() << QDir::currentPath();
return 0;
}




C:\Documents and Settings\JP\My Documents\Visual Studio Projects\CurrentDir\Debug>CurrentDir.exe
"C:/Documents and Settings/JP/My Documents/Visual Studio Projects/CurrentDir/Debug"

C:\Documents and Settings\JP\My Documents\Visual Studio Projects\CurrentDir\Debug>cd ..

C:\Documents and Settings\JP\My Documents\Visual Studio Projects\CurrentDir>Debug\CurrentDir.exe
"C:/Documents and Settings/JP/My Documents/Visual Studio Projects/CurrentDir"

C:\Documents and Settings\JP\My Documents\Visual Studio Projects\CurrentDir>cd ..

C:\Documents and Settings\JP\My Documents\Visual Studio Projects>CurrentDir\Debug\CurrentDir.exe
"C:/Documents and Settings/JP/My Documents/Visual Studio Projects"

L.Marvell
7th April 2006, 16:02
An application can be launched from a different directory where the executable itself lies.
"Current dir" is assumably the directory where the executable was launched from.
So I wouldn't suggest using that kind of approach..

Maybe you could just use QSettings instead? Or alternatively QDir::homePath()..
(Note that the latter might be problematic on windows, though.)
Oops... You are right. I didn't think about this case. :o

jrideout
7th April 2006, 16:04
Try this:


QString QCoreApplication::applicationDirPath () [static]
Returns the directory that contains the application executable.
For example, if you have installed Qt in the C:\Trolltech\Qt directory, and you run the regexp example, this function will return "C:/Trolltech/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).
Warning: On Unix, this function assumes that argv[0] contains the file name of the executable (which it normally does). It also assumes that the current directory hasn't been changed by the application.From Qt docs

Dark_Tower
7th April 2006, 16:19
Let's look what Assistant says:

It seems it does ;)

I also have looked at the docs... The problem is the one that jpn has mentioned.

L.Marvell
7th April 2006, 16:36
I also have looked at the docs... The problem is the one that jpn has mentioned.
Yep, I've already seen. It's quite interesting but jpn's code does nothing to me. I never wrote console apps with Qt, maybe I do something wrong. I've just copied code and did standard qmake -project, qmake, make. Result of running app:

C:\temp\test\release>test.exe


C:\temp\test>
P.S. Forgot to say. Using Qt 4.1.2 with MinGW

Dark_Tower
7th April 2006, 17:03
L.Marvell you have to start jpn's code on debug mode or in other case show the current path with a message box

L.Marvell
7th April 2006, 17:59
L.Marvell you have to start jpn's code on debug mode or in other case show the current path with a message box
I wish to help you, but you helped me :) Thanks!

Dark_Tower
7th April 2006, 18:23
I wish to help you, but you helped me :) Thanks!

I'm thankful for your good intentions, too :)