[英]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"
. 我之所以这样问,是因为我想拥有一个资产文件夹,相对于必须输入
"absolute/path/to/assets"
,我可以通过"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. 最坏的情况是,我可以创建一个宏或const std :: string / QString附加到所有内容,但是我发现,允许项目能够相对于源目录访问所有内容相对更清洁实际的构建目录。
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. 我还应该声明我在Linux上运行,并且目前没有任何用于多平台的计划,这主要是由于我只是在创建研究项目而已。
Switch your working directory to the application executable's directory, like so: 将工作目录切换到应用程序可执行文件的目录,如下所示:
if (QDir::setCurrent(QCoreApplication::applicationDirPath())) {
// success!
...
}
This is cross-platform. 这是跨平台的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.