使用qmake复制文件
您可以使用 qmake 将图像文件复制到构建目录,以便应用程序可以找到它们。我目前在我的项目中使用this solution,因为我不想使用 make install。您应该阅读该答案以理解它,但在这里给出一些上下文:
# 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
正如答案所说,这仅适用于一个文件,因此如果要复制整个图像目录,则需要使用$(COPY_DIR)。这可能不是最好的方法,但是 qmake 很难使用,所以当它对我有用时,我很高兴。 :)
一旦你得到了这个工作,你可以使用this answer来引用文件系统上的图像:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
int main(int argc, char *argv[]) {
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("applicationDirPath", QGuiApplication::applicationDirPath());
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec(); }
QML:
Image {
source: "file:///" + applicationDirPath + "/images/image.png"
}
这会设置一个 QML 上下文属性,该属性指向运行可执行文件的目录,然后使用该目录形成图像的路径。
rcc 的压缩选项
请注意,如果您的大部分或所有图像都是 PNG,您可以查看 rcc 的 compression options 是否有助于确定可执行文件的大小:
资源默认被压缩(ZIP 格式)。它是
可以关闭压缩。如果您的资源
已包含压缩格式,例如 .png 文件。你做这个
通过提供 -no-compress 命令行参数。
rcc -no-compress myresources.qrc
rcc 还可以让您对压缩进行一些控制。您可以指定
要考虑的压缩级别和阈值级别
压缩文件,例如:
rcc -compress 2 -threshold 3 myresources.qrc