I want to get a path to the project directory in Qt and reference the other files of my project relative to it. I know this issue have been already discussed here Get current working directory in a Qt application and here How to get current working directory path of a Qt application? but the answer is still unknown. In case it's impossible to get such a path then what is the approach of navigation among files of the Qt project.
Asked
Active
Viewed 2,018 times
0
-
Do you need path to the binary file of your application, or path to the .pro file? – Bearded Beaver Apr 01 '17 at 19:34
-
path to the .pro file – Artem Zefirov Apr 01 '17 at 19:47
-
1`$$PWD` is for project working directory (in .pro file) – Bearded Beaver Apr 01 '17 at 19:58
-
If your executable has the same name than the directory of your working directory, this work for me: QFileInfo(".").absolutePath() + "/" + QApplication::applicationName() – Gabrielle de Grimouard Apr 01 '17 at 20:14
-
This command yields for me the path to binary files. How one can acquire value of $$PWD? – Artem Zefirov Apr 01 '17 at 20:32
-
For what reason do you want to do this? There may be a better way with either using a resource container or simply copying the file(s) to your build target directory – HorusKol Apr 02 '17 at 03:54
-
To access some images are being used in my programme I could of course put them into build target directory, but it becomes uncomfortable to pass my code to others. So, I decided to use a path relative to the build target directory until would find more elegant decision. – Artem Zefirov Apr 02 '17 at 07:11
1 Answers
2
Based on your comment, you are trying to:
Access some images which are being used in my program. I could of course put them into build target directory, but it becomes uncomfortable to pass my code to others.
The approach to store resource files in the project source directory and rely on such structure at runtime is not a greatest idea. I can't imagine the situation when mixing the concepts of initially decoupled source and build directories could be useful, correct me if I'm wrong.
So, according to your needs:
- The most simple and plain way is to use the Qt resource system. The resource files are simply embedded into the executable, so there will be no need to access the file system.
- Another way is the automatic deployment of the needed files. This answer describes the way to copy your files to the target directory using
qmake
.