简体   繁体   English

Qt资源系统中的前缀优于文件系统路径的优点是什么?

[英]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 Qt资源文件.qrc允许将嵌入文件拆分为不同的前缀

<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 . 在这两种情况下,您都可以在C ++中以相同的语法使用该文件:/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. 这只是将资源ID与实际文件系统对象分离的一种方法。 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: 假设您拥有许多应用程序之间共享的映像存储库,则可以像这样在qrc中引用文件:

<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: 如果需要其他徽标,或者将存储库移至其他位置,则仅需编辑qrc文件:

<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. 该技术反映了C ++或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: 然后,应实施qt资源重新映射技术,该技术如下所示:

|---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. 注意:因为您已将所有文件放入一个逻辑文件夹(实际上是同一名称空间),所以文件名不应相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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