Search code examples
pythonpyqt4embedded-resource

resource file in PyQt4


I'm trying to understand an example in PyQt4 (simpletreemodel.pyw) I see the code

import simpletreemodel_rc

But I can't see where the module is used in the example code When I examine the module simpletreemodel, I see:

    from PyQt4 import QtCore

qt_resource_data = b"\
\x00\x00\x07\xb9\
\x47\
\x65\x74\x74\x69\x6e\x67\x20\x53\x74\x61\x72\x74\x65\x64\x09\x09\
\x09\x09\x48\x6f\x77\x20\x74\x6f\x20\x66\x61\x6d\x69\x6c\x69\x61\
\x72\x69\x7a\x65\x20\x79\x6f\x75\x72\x73\x65\x6c\x66\x20\x77\x69\
\x74\x68\x20\x51\x74\x20\x44\x65\x73\x69\x67\x6e\x65\x72\x0a\x20\
\x20\x20\x20\x4c\x61\x75\x6e\x63\x68\x69\x6e\x67\x20\x44\x65\x73\
\x69\x67\x6e\x65\x72\x09\x09\x09\x52\x75\x6e\x6e\x69\x6e\x67\x20\
\x74\x68\x65\x20\x51\x74\x20\x44\x65\x73\x69\x67\x6e\x65\x72\x20\

What this module is supposed to do? Thanks


Solution

  • What you see is the byte-by-byte dump of the resources the .qrc file contains. You don't explicitly access the objects inside the module. Just import it, and you will be able to access those resources by their original names(and paths) but preceded by a colon.

    pixmap = QPixMap(':/images/filename.jpg')
    

    UPDATE: QRC file is an XML file that looks like below:

    <RCC>
      <qresource prefix="/images">
        <file alias='filename.jpg'>images/filename.jpg</file>
      </qresource>
    </RCC>
    

    Then to generate it, use:

    pyrcc4 -o images_rc.py images.qrc