Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 816
biqqles Avatar asked Feb 02 '26 05:02

biqqles


1 Answers

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 resources.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 libraries 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:

  • How to read a (static) file from inside a Python package?
like image 118
ekhumoro Avatar answered Feb 03 '26 22:02

ekhumoro