Reliable way to read a file in the project directory
-
Hey Everyone!
I'm trying to read a file in called "CQF.json" in the "data/" directory of my project. "data/CQF.json" is in my resourses.qrc file. I've found that setting my current directory to "../.." exits the build/<SPECIFIC_BUILD> directory and puts me in the directory of my project, allowing me to open the file I want, like so:
QDir::setCurrent("../.."); QFile file("data/CQF.json"); bool success = file.open(QIODevice::ReadOnly);
The code above works as far as I can tell, but I'm wondering if there is a better way to do this.
- Is there a way to set the directory to the base project directory which doesn't involve exiting the build directory?
- Is there a way to read this file without having to worry about the current directory?
- Or is the way I presented about the standard method of reading such a data file?
Any tips / help appreciated!
-
Put the file into a Qt resource and read it from there.
-
Put the file into a Qt resource and read it from there.
@Christian-Ehrlicher Perfect, thanks!
I have changed the code to the following:
QFile file(":/data/CQF.json"); bool success = file.open(QIODevice::ReadOnly);
-