Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt: Path to data directory

Tags:

c++

qt

I have a Qt application with a large amount of data stored in a folder called "Data", which lies in the same directory as the applications qrc file.

I want to somehow have the application know where to find this folder. I don't want to add all of the data as resource files because I want to be able to add and remove new data files into the Data directory without having to recompile every time. I don't want to hardcode relative file paths because the execution directory can differ depending on operating system, how the application is launched etc, and I don't want to hardcode absolute directories because they won't be the same if the application is compiled on different machines.

How can I make my application know where to look for the data folder? The directory structure of the entire project will be the same regardless of what OS/machine it is on.

like image 294
kazimpal Avatar asked Sep 06 '25 09:09

kazimpal


1 Answers

The application directory is available through QCoreApplication.applicationDirPath(), available via the global qApp:

  #include <QApplication>

  ...
  QString appDir = qApp.applicationDirPath();
like image 172
Andy Thomas Avatar answered Sep 09 '25 05:09

Andy Thomas