frankyD
frankyD

Reputation: 33

QML + .qrc: Keeping images under another prefix

My problem has been similar to that discussed in another stackoverflow discussion and I could get my code working that way. However, I'm not completely satisfied with that solution. Initially, I had my .qml-file under one prefix ("/") and my images under another ("/images"). Without abandoning this separation, I don't get the program running.

Is there any (simple) way to use different prefixes in a QML project with .qrc resource file?

Upvotes: 2

Views: 3881

Answers (4)

OnurA
OnurA

Reputation: 651

In your case, instead of writing "images" twice as your source path, you could have defined the file in your resource file as follows:

<file alias="foo.png">images/foo.png</file>

Then you are allowed to use "images" only once.

Window {
visible: true
Image {
    source: "qrc:/images/foo.png"
}

}

See the documentation!

Upvotes: 0

Alfredo Pons Menargues
Alfredo Pons Menargues

Reputation: 166

Instead of ???, you must put the path "qrc:/images/images/foo.png"

Example:

//main.qml
import QtQuick 2.2
import QtQuick.Window 2.0

Window {
    visible: true
    Image {
        source: "qrc:/images/images/foo.png"
    }
}

Upvotes: 1

tiktok
tiktok

Reputation: 456

If you want to group the files by prefix based on the actual folder name without lengthening the reference name then why not use the alias keyword?

<RCC>
<qresource prefix="/">
    <file>main.qml</file>
</qresource>
<qresource prefix="/images">
    <file alias="foo.png">images/foo.png</file>
</qresource>
</RCC>

This will remove the extra indirection that was causing confusion i.e. you can access it with :/images/foo.png instead of :/images/images/foo.png

An explanation and further examples are available in the Qt docs (search for "alias"): http://doc.qt.io/qt-5/resources.html

Upvotes: 2

frankyD
frankyD

Reputation: 33

Instead of asking, I should have gone drinking a coffee or do some sports. It's an embarrassing beginner's problem. Still, there might be others like me...

My QtQuick application consisted of essentially of a C++ source file main.cpp, a resource file qml.qrc and an image foo.png.

Source file (the shown code is generated automatically by QtCreator):

//main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
    return app.exec();
}

Resource file with additional prefix for images:

//qml.qrc
<RCC>
<qresource prefix="/">
    <file>main.qml</file>
</qresource>
<qresource prefix="/images">
    <file>images/foo.png</file>
</qresource>
</RCC>

A qml file, where I want to import an image:

//main.qml
import QtQuick 2.2
import QtQuick.Window 2.0

Window {
    visible: true
    Image {
        source: ???
    }
}

My problem was that I didn't know what to write instead of ??? in the .qml file. To import the graphic you need to write "/images/images/foo.png", but my mind revolted against the idea of writing /images twice.

Thanks.

Upvotes: 1

Related Questions