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

落花浮王杯 提交于 2021-02-11 06:51:12

问题


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?


回答1:


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



回答2:


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?


来源:https://stackoverflow.com/questions/66099225/how-can-resources-be-provided-in-pyqt6-which-has-no-pyrcc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!