As The Qt Resource System
Documentation says,
the main reason of using resource file is to avoid the risk of losing the files.
pyrcc5
generates all in one binary-like string which contains all resource files defined in .qrc
file, and adds that string to .py
file (in this case, resources.py
), then you import that module (from .resources import *
or from pluginname.resources import *
)
When you add -for example- a new 'icon' to .qrc
file or modify the icon file content, you have to run pyrcc5
again to modify resources.py
.
I work on different OS and computers. I don't prefer to use .qrc
file not to run pyrcc5
again or to avoid getting errors with pyrcc5
. Instead, I use full path of the file using the following structure. That's personal choice.
Usage without resource file:
plg_dir = os.path.dirname(__file__)
# icons: folder containing my icon files
icon_path = os.path.join(plg_dir, "icons", "zoom.png")
# OR icon_path = plg_dir + '/icons/zoom.png'
icon = QIcon(icon_path)
Usage with resource file:
Before, you should genereta resource.py
file using pyrcc5
.
from .resources import *
.
.
icon_path = ':/icons/zoom.png'
icon = QIcon(icon_path)
Content of resources.qrc:
<!DOCTYPE RCC>
<RCC version="1.0">
<qresource>
<file>icons/zoom.png</file>
...
</qresource>
</RCC>