0

I need to load images in my qml window. Usually I load it from a resource file which will be deployed along with my .pro file. But since the image folder size is too big the loading time is more. Is there any way by which a local folder can deploy with the qtquick application?. And how can I refer images in qml file.(The path of image). I am using Qt 5.5 version.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Neethu
  • 69
  • 1
  • 11
  • you could explain what you want to say with the following question: *Is there any way by which to local folder can deploy with the qtquick application?* – eyllanesc Jan 08 '18 at 06:55
  • https://stackoverflow.com/questions/11593165/qtcreator-copy-files-to-output-directory-with-installs is the answer. Thanks for comment! – Neethu Jan 08 '18 at 09:52

1 Answers1

0

Using qmake to copy files

You can use qmake to copy the image files to the build directory so that the application can find them. I'm currently using this solution in my project because I don't want to use make install. You should read that answer to understand it, but to give it some context here:

# 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

As the answer says, this is just for one file, so you'll need to use $(COPY_DIR) if you want to copy a whole directory of images over. This is probably not the best approach, but qmake is difficult to use, so when it works for me, I'm happy. :)

Once you've got that working, you can use this answer to refer to the images on the file system:

#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"
}

This sets a QML context property that points to the directory that the executable is running in, and then uses that directory to form the path to the image.

rcc's Compression Options

Note that if most or all of your images are PNGs, you could see if rcc's compression options help with the size of the executable:

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
Mitch
  • 23,716
  • 9
  • 83
  • 122