Search code examples
c++windowsqtembedded-resource

Get the container file for an embedded resource in Qt C++


Here it is shown how to get list of files stored in a .qrc Qt Resorce file?.

This solution works as expected, however the list does not show the .qrc file from which the given entry originates, as in the case where the project contains several sub-projects in the form of libraries, each with its own .qrc file.

So, the question is: Is there a way to know to which .qrc file the given resource in the obtained list belongs?

I need this information to make the library aware of which resources are available from its local .qrc, not to get all the resources, which are available globaly for the application.

Platform info: Qt 5.6.1 with MSVC2013 installed on Windows 7


Solution

  • No, you can't. When the qrc file compiled, the original file name won't be incorporated into compiled binary. If you want the library awares of its local resource, you should add prefix to each resource file with library identifier. For more explanation see Qt Resource System. For example

    <!-- resource for mylib01 -->
    <!DOCTYPE RCC><RCC version="1.0">
    <qresource prefix="mylib01">
        <file>images/copy.png</file>
        <file>images/cut.png</file>
    </qresource>
    </RCC>
    
    
    <!-- resource for mylib02 -->
    <!DOCTYPE RCC><RCC version="1.0">
    <qresource prefix="mylib02">
        <file>images/copy.png</file>
        <file>images/cut.png</file>
        <file>images/new.png</file>
    </qresource>
    </RCC>
    

    Each resource file then will be available as :/mylib01/copy.png, :/mylib02/copy.png, ... When listing files in the resource for a specific library, simply compare the library identifier with resource file name to check whether it is belong to the library or not.

    Another option is using separate binary resource, then load it dynamically with QResource. You can access the compiled resource file name by QResource::fileName().