The documentation for PyQt6 states that
Support for Qt’s resource system has been removed (i.e. there is no
pyrcc6).
In light of this, how should one provide resources for a PyQt6 application?
The documentation for PyQt6 states that
Support for Qt’s resource system has been removed (i.e. there is no
pyrcc6).
In light of this, how should one provide resources for a PyQt6 application?
There has been some discussion on the PyQt mailing list when this was found out.
The maintainer is not interested in maintaining pyrcc anymore as he believes that it doesn't provide any major benefit considering that python already uses multiple files anyway.
The easiest solution is probably to use the static methods of QDir setSearchPaths() or addSearchPath().
The difference will be that resources will be loaded using the prefix used for the methods above.
Considering the previous situation:
icon = QtGui.QIcon(':/icons/myicon.png')
Now it would become like this:
# somewhere at the beginning of your program
QtCore.QDir.addSearchPath('icons', 'path_to_icons/')
icon = QtGui.QIcon('icons:myicon.png')
UPDATE:
As of PyQt-6.3.1, it's possible to use Qt’s resource system again.
This version now includes the qRegisterResourceData and qUnregisterResourceData functions which are required by the generated python resource module. The rcc tool supplied with Qt6 must now be used to generate the module, but since this tool only supports PySide6, the imports must be adjusted to work with PyQt6. The module can be created like this:
rcc -g python -o resources.py resources.qrc
which outputs a file with a header like this:
# Resource object code (Python 3)
# Created by: object code
# Created by: The Resource Compiler for Qt version 6.3.1
# WARNING! All changes made in this file will be lost!
from PySide6 import QtCore
This file is essentially the same as what was once produced by pyrcc. All that remains is to either re-write the file with a PyQt6 import line, or update sys.modules to use the right QtCore module (which obviously must be done before importing the resource module):
from PyQt6 import QtCore
sys.modules['PySide6.QtCore'] = QtCore
OLD ANSWER:
The consensus seems to be that the existing python facilities should be used instead of pyrrc. So the resources would be stored directly in the file-system (perhaps within archive files), and then located using importlib.resources (python >= 3.7), or pkg_resources, or a third-party solution like importlib_resources. Exactly how this maps to existing uses of pyrcc will probably be application-specific, so some experimentation will be needed to find the best approach.
For more details on how to use these facilities, see:
As I started to use PyQt6, I found missing full support for Qt6 Resources. Especially when using designer and using images for buttons, labels etc. I tried addSearchPath, but still had to edit generated .py template. After some research I found using importlab the best solution for my problem.
I made simple script, which is using .qrc file and generates .py templates with importpath.
For example changing:
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(":/icons/icon1.png"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
to:
icon = QtGui.QIcon()
with path("myPackage.resources.icons", "icon1.png") as f_path:
icon.addPixmap(QtGui.QPixmap(str(f_path)), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
Here is a link to GitLab repo: https://github.com/domarm-comat/pyqt6rc
Or install via pip:
python3 -m pip install pyqt6rc