c++qtqt-resource

What is the advantage of prefix over filesystem path in Qt resource system?


The Qt resource file .qrc allows to split the embedded files into different prefixes

<RCC>
    <qresource prefix="/qml">
        <file alias="CustomWidget.qml">qml/CustomWidget.qml</file>
    </qresource>
    <qresource prefix="/icons">
        <file alias="home.png">icons/home.png</file>
    </qresource>
</RCC>

I often see developers redoing the filesystem hierarchy with prefixes like the example above. But in my opinion it is exactly the same as this for the caller code point of view :

<RCC>
    <qresource>
        <file>qml/CustomWidget.qml</file>
        <file>icons/home.png</file>
    </qresource>
</RCC>

In both cases you can use the file in C++ with the same syntax :/qml/CustomWidget.qml.

Is there any advantage at using the prefix+alias over the filesystem path ?


Solution

  • It's just a way to decouple resources id's from the actual filesystem objects. Once you have defined a prefix and an alias to reference a resource, even if the resource file changes (i.e. is substituted by another file with a different name and path) the code is left untouched.

    Say you have a repository of images shared among many applications, you may reference files in a qrc like this:

    <RCC>
        <qresource prefix="/pics">
            <file alias="logo">../../../../pictures/logos/logo-001.png</file>
        </qresource>
    </RCC>
    

    Code is pretty much agnostic on the long file path, the resource is referenced just like this:

    QPixmap pix(":/pics/logo");
    

    If a different logo is needed, or the repository has been moved to a different location, only the qrc file has to be edited:

    <RCC>
        <qresource prefix="/pics">
            <file alias="logo">../../../new-repo/logos/logo-002.png</file>
        </qresource>
    </RCC>