我有幸浪费了几个小时,我以为'd share my findings on the matter. This in a modified variant of Paglian'方法here . 自从我'm using windows (without mingw) that method doesn'工作 . 所以这是修改后的变体:
# using shell_path() to correct path depending on platform
# escaping quotes and backslashes for file paths
copydata.commands = $(COPY_FILE) \"$$shell_path($$PWD\\archive.png)\" \"$$shell_path($$OUT_PWD)\"
first.depends = $(first) copydata
export(first.depends)
export(copydata.commands)
QMAKE_EXTRA_TARGETS += first copydata
由于这使它成为跨平台,你当然也可以在Linux,MacOS或你拥有的东西中使用这种方法 . 请注意我正在复制单个文件,因此我使用 $(COPY_FILE) 而不是 $(COPY_DIR) . 根据需要进行调整 .
如果你想将文件复制到二进制文件最终的确切路径(因为二进制文件将在$$ OUT_PWD的子文件夹中结束(调试或发布,至少在使用带有MSVC 14 / cdb的Qt Creator构建时) .exe / Code :: Blocks Makefiles配置)你需要这个:
# adapted from https://stackoverflow.com/a/2581068
CONFIG(debug, debug|release) {
VARIANT = debug
} else {
VARIANT = release
}
请注意,即使二进制文件在子文件夹中结束,QtCreator也会从 $$OUT_PWD 执行二进制文件,因此它希望在 $$OUT_PWD 中找到文件资源,而不是调试子文件 . 这意味着你例如不能做 QIcon("archive.png") 并期望它除了可执行文件之外找到它 .
这当然可以通过以下方式轻松解决:
QDir exeDir(QCoreApplication::applicationDirPath());
QIcon qIcon(exeDir.filePath("archive.png"));
如果您认为这是您想要的,您显然需要编辑 $$(COPY_FILE) (in .pro)的最后一个参数,如下所示: \"$$shell_path($$OUT_PWD)\\$$VARIANT\"
另外需要注意的是(在我的情况下,无论如何)Qt Creator(4.0.1)没有检测到配置中的任何更改,因此要在Makefile中反映上述更改(因此在构建项目时运行) )您必须通过从应用程序菜单运行 Build->run qmake 来手动构建.pro . 为确保一切顺利,请按Alt 4(在Windows上)查看编译输出 .