4

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?

ekhumoro
  • 107,367
  • 18
  • 208
  • 308
biqqles
  • 183
  • 1
  • 10

3 Answers3

6

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')
musicamante
  • 31,222
  • 5
  • 26
  • 48
  • Thanks, this is a good solution since it requires minimal modification of any existing code. I imagine the alternative (using a generic resource system) must require something like `QIcon(QPixmap.loadFromData(data))` - and that wouldn't work for vector images. – biqqles Feb 08 '21 at 19:40
  • @biqqles not necessarily: using the [solutions provided by ekhumoro](https://stackoverflow.com/a/66104738/2001654) will still get you access to the file paths in the correct way. Take a look a the whole mail thread linked in the answer, IIRC there should be a couple of messages about that. – musicamante Feb 08 '21 at 19:55
  • I looked through the thread again - do you mean [this message](https://www.riverbankcomputing.com/pipermail/pyqt/2020-September/043228.html) with links to qutebrowser's code? I think I still prefer the neatness of `addSearchPath` but it may well be less robust when it comes to packaging. Both solutions are good but I could only pick one! – biqqles Feb 13 '21 at 21:39
  • When you create an `exe` with `pyinstaller`, then the `path_to_icons` contents are automatically load to the archieve (or must be putted in the exe folder and if yes where)? – Chris P Jan 28 '22 at 22:27
3

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:

ekhumoro
  • 107,367
  • 18
  • 208
  • 308
0

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
Domarm
  • 1,875
  • 1
  • 3
  • 14