PDA

View Full Version : Problems with the current working directory.



aarelovich
19th May 2011, 19:34
I'm writing a program using QtCreator that uses some files in a folder (data) in the same folder as the project folder. I'm working under linux.

In the constructor I have a line that is QFile file("./data/file.ext"). When I execute the program in the creator everything is fine.

However it fails when I launch it by double clicking the created executable because the the program thinks that it is in /home/user/Documents. (I checked by having a button that creates a message with the output of QDir::current().absoulutePath().

My question is why? and how can I change the working folder to the folder where the program executes?

Thank you very much for any input.

squidge
19th May 2011, 19:41
The current directory can be anything upon startup, but you can always find the path to your applications executable via argv[0]

Your shell most likely sets the cwd to /home/user/Documents so that directory is shown by default for any file saving/loading (a sensible decision - users will not want to manually select it everytime to save there work)