如何将本地文件夹附加到qtquick
how to attach local folder into qtquick
我需要在我的 qml 中加载图像 window。通常我从一个资源文件加载它,该文件将与我的 .pro 文件一起部署。但是由于图像文件夹太大,加载时间更长。有什么方法可以通过 qtquick 应用程序部署本地文件夹吗?以及如何在 qml 文件中引用图像。(图像的路径)。我正在使用 Qt 5.5 版本。
使用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 答案来引用文件系统上的图像:
#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 是否有助于确定可执行文件的大小:
Resources are compressed by default (in the ZIP format). It is
possible to turn off compression. This can be useful if your resources
already contain a compressed format, such as .png files. You do this
by giving the -no-compress command line argument.
rcc -no-compress myresources.qrc
rcc also gives you some control over the compression. You can specify
the compression level and the threshold level to consider while
compressing files, for example:
rcc -compress 2 -threshold 3 myresources.qrc
我需要在我的 qml 中加载图像 window。通常我从一个资源文件加载它,该文件将与我的 .pro 文件一起部署。但是由于图像文件夹太大,加载时间更长。有什么方法可以通过 qtquick 应用程序部署本地文件夹吗?以及如何在 qml 文件中引用图像。(图像的路径)。我正在使用 Qt 5.5 版本。
使用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 答案来引用文件系统上的图像:
#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 是否有助于确定可执行文件的大小:
Resources are compressed by default (in the ZIP format). It is possible to turn off compression. This can be useful if your resources already contain a compressed format, such as .png files. You do this by giving the -no-compress command line argument.
rcc -no-compress myresources.qrc
rcc also gives you some control over the compression. You can specify the compression level and the threshold level to consider while compressing files, for example:
rcc -compress 2 -threshold 3 myresources.qrc