How can resources be provided in PyQt6 (which has no pyrcc)?

Question:

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?

Asked By: biqqles

||

Answers:

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')
Answered By: musicamante

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.)

There’s still no pyrcc6 tool, but Qt’s own rcc tool can now be used to convert the qrc file. This tool should be installed by default with a full Qt6 installation, but if you can’t find it, you could also use the PySide6 tools to convert the qrc file. (PySide6 simply searches for the Qt6 rcc tool and runs it using subprocess, so it will produce exactly the same output).

Thus, to convert the qrc file, you can now use either:

rcc -g python -o resources.py resources.qrc

or:

pyside6-rcc -o resources.py resources.qrc

However, it’s very important to note that the import line at the top of the generated file must be modified to work correctly with PyQt6:

# Resource object code (Python 3)
# Created by: object code
# Created by: The Resource Compiler for Qt version 6.4.0
# WARNING! All changes made in this file will be lost!

# from PySide6 import QtCore <-- replace this line
from PyQt6 import QtCore

The whole operation can be done with this unix one-liner (requires GNU sed):

rcc -g python resources.qrc | sed '0,/PySide6/s//PyQt6/' > resources.py

or:

pyside6-rcc reources.qrc | sed '0,/PySide6/s//PyQt6/' > resources.py  

Once this small change has been made, the generated module can be safely imported into the main application, like this:

from PyQt6 import QtCore, QtGui, QtWidgets
from test_ui import Ui_Window
import resources

class Window(QtWidgets.QWidget, Ui_Window):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

if __name__ == '__main__':

    app = QtWidgets.QApplication(['Test'])
    window = Window()
    window.show()
    app.exec()

Note that it is NOT SAFE to use the generated module without making the changes noted above. This is because the unmodfied module will attempt to import PySide6, which is obviously inappropriate for a PyQt6 application. Whilst it may seem to work on the development machine, there’s no guarantee that mixing the two libararies in this way will always remain compatible – and in any case, it’s bad practice to enforce the installation of PySide6 on a user’s system just so that they can run a PyQt6 application.


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:

Answered By: ekhumoro

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
Answered By: Domarm

for those people who want a real and simple solution just watch it here: link

a guy figured it out by converting the "resource.qrc" into a .py file by using the pyrcc of PySide6. then importing the resource.py (same as before) in your PyQt6 project. everything is the same, including the special filepath syntax: ":/image.jpg" instead of "./image.jpg"

hope it helps, always feels good to have a simpler solution.

Answered By: adfinem_rising

The owner decided pyrcc6 wasn’t useful and no longer provides it. However, what he doesn’t understand is how useful it is for those of us that use qt designer to define our resources like icons.. etc and then using pyinstaller to package and find all resources, so that pyinstaller can build a stand-alone exe and these icons are properly embedded and used in the .ui user interface.

You may find this link useful (I had no success with it though): https://pypi.org/project/pyqt6rc/

Ultimately, even though I am using pyqt6, I also installed pyside6 using pip and used pyside6-rcc command to do the same thing that the old pyrcc5 command used to do.

The best full explanation can be found at this YouTube link:

https://www.youtube.com/watch?v=u5BLPTkbaM8

Answered By: panofish

Adding further to youtube video (Update 28/12/22) https://www.youtube.com/watch?v=u5BLPTkbaM8

Do it with the simple script:

pyuic6 tip.ui > tip.py && sed -i '10iimport _cf_rcnimport _rc_rc' tip.py

Explaination here:

sed -i "10i' -> means insert from the 10th line onwards.

I have 2 files to insert in tip.py:

import _cf_rc

import _rc_rc
Answered By: Rishabh Bhardwaj
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.