Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current working directory in a Qt application

Tags:

c++

qt

I'm writing a program in C++ using the Qt library. There is a symbolic link in my home bin directory to the executable. I would like the current working directory of my program to be the directory in which I am with my terminal (ie. the result of the pwd command). I saw the QDir::currentPath() function, but it gives back the directory where the binary is.

How can I find my current working directory?

like image 454
Geoffroy Avatar asked Sep 13 '11 13:09

Geoffroy


People also ask

How do I get current working directory in QT?

Yes : QDir dir; Cout << dir. absolutePath() << flush; gives me the directory where the executable lives. Both QDir::currentPath() and dir. absolutePath() return the current directory of the command line.

How do you create a relative path in Qt?

Solution 2QDir dir("../MyProjects/ProjectB"); If the current working directory is not a sub directory of "C:\MyDevelopment\", the path will be probably invalid (not existing). This will return the path to the passed name relative to the above directory. LFName= dir.


1 Answers

Just tested and QDir::currentPath() does return the path from which I called my executable.

And a symlink does not "exist". If you are executing an exe from that path you are effectively executing it from the path the symlink points to.

like image 111
RedX Avatar answered Oct 08 '22 11:10

RedX