简体   繁体   中英

Qt Creator/qmake - Load assets folder from source directory, as opposed to build directory

Is there a way to specify the root directory at runtime within a program's execution? The reason why I ask is because I'd like to be able to have an assets folder which I can access in code simply via "assets/" relatively, as opposed to having to type "absolute/path/to/assets" . Worst case scenario I can create a macro or a const std::string/QString to append to everything, but I find that it would be cleaner to just allow the project to be able to access everything relatively FROM the source directory, as opposed to the actual build directory.

How can this be achieved, if at all?

edit

I should also state that I'm operating in Linux, and don't have any plans for multi-platform use at the moment, mainly due to the fact that I'm just creating study projects.

Switch your working directory to the application executable's directory, like so:

if (QDir::setCurrent(QCoreApplication::applicationDirPath())) {
  // success!
  ...
}

This is cross-platform.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM