简体   繁体   中英

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 ?

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 (ie 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>

This behaviour re-maps the file-path onto a logic-space-path, meaning that even-though you have a-lot-of files residing in different paths, they can-be combined-into one logic-folder.

This technology reflects the same concept of namespace in C++ or Java.

For example:

You have following files, but they are-not in the same folder.

|---bar
   |---ca.cc
   |---da.cc
|---foo
   |---fa.cc
   |---ga.cc

For convenience, you need-to make file-access more consistent and not-to-be influenced-by the files' location changing.

Then, qt resource re-mapping technology should implemented, which will look-like following:

|---uniFolder
   |---ca.cc
   |---da.cc
   |---fa.cc
   |---ga.cc

Notice: Because you have put all files into one logic-folder(Actually a same namespace), so names of files shouldn't-be the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM