简体   繁体   English

Qt Creator / qmake-从源目录(而不是构建目录)加载资产文件夹

[英]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.

相关问题 qt creator 3.6(从源代码构建)在构建任何qt app项目时无法启动进程qmake - qt creator 3.6 (build from source) cannot start process qmake when build any qt app project 将CWD设置为Source Directory,而不是Build Directory - Set CWD to Source Directory, as opposed to Build Directory 尝试使用 MSVC 从源代码构建 qt 创建者:无法打开包含文件:'stddef.h':没有这样的文件或目录 - Trying to build qt creator from source with MSVC: Cannot open include file: 'stddef.h': No such file or directory 将 assets 目录从源代码复制到 CMake 中的构建目录 - Copying assets directory from source to build directory in CMake 在 Qt Creator 中禁用“构建目录需要与源目录处于同一级别”警告 - Disable "The build directory needs to be at the same level as the source directory" warning in Qt Creator Qt Creator构建项目 - 没有这样的文件或目录 - Qt Creator build project - No such file or directory Qt Creator项目更改目录 - qt creator project change directory 使用Qt创建者查找翻译目录 - Find the directory of a translation with Qt creator PRECOMPILED HEADERS无法使用Qt Creator / qmake / MinGw进行调试构建 - PRECOMPILED HEADERS not working in debug build with Qt Creator / qmake / MinGw 无法构建,qmake.exe在Qt Creator中崩溃 - Can't build, qmake.exe crashes in Qt Creator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM