Import resource file into PyQt code?

I saw a Qt documentary and many questions similar to this one, but I still haven't figured out how to do this.

I'm not quite sure how I can import a resource file into Python code, so pixmap appears without any problems.


I have all the files in one directory, I created qrc. file and compiled it with rcc -binary resources.qrc -o res.rcc to create a resource file.

I imported res_rcc, but the pixmap on the shortcut still didn't display:

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 into PyQt code? | If pixmaps are in the same directory as the .qrc resource files, do I still need to specify the full path?

+5
source share
1 answer

For pyqt, you should use pyrcc4, i.e. the rcc equivalent for python.

 pyrcc4 -o resources.py resources.qrc 

This creates a resources.py module that needs to be imported into python code to make resources available.

 import resources 

To use a resource in code, you must use the prefix ": /":

Example

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

See PyQt4 Resource System and Qt Resource System

+9
source

Source: https://habr.com/ru/post/1247300/


All Articles