Search code examples
pythonqtpyqtpyqt4

Importing resource file to PyQt code?


I have seen Qt documentary and a lot of questions less-similar to this one, But i still haven't figured out how can i do it.

I'm not entirely sure how can i import resource file to Python code, so pixmap appears without any issues.


I have all files in same directory, I created qrc. file and compiled it with: rcc -binary resources.qrc -o res.rcc to make resource file.

I imported res_rcc but pixmap on label was still not shown:

import res_rcc


This is what i had in my qrc. file:

<RCC>
  <qresource prefix="newPrefix">
    <file>download.jpeg</file>
  </qresource>
</RCC>

Question:

How can i import resource files in the PyQt code ? | If pixmaps are in same directory as .qrc resource files, Do i still need to specify full path?


Solution

  • For pyqt you have to use pyrcc4, which is the equivalent of rcc for python.

    pyrcc4 -o resources.py resources.qrc
    

    This generates the resources.py module that needs to be imported in the python code in order to make the resources available.

    import resources
    

    To use the resource in your code you have to use the ":/" prefix:

    Example

    from PyQt4.QtCore import *
    from PyQt4.QtGui import *
    
    import resources
    
    pixmap = QPixmap(":/newPrefix/download.jpeg")
    

    See The PyQt4 Resource System and The Qt Resource System