英文:
Common resource file in PyQt/ PySide and resource file location
问题
当通过Qt Designer在表单中创建资源文件时,Qt Designer生成的Python代码包括以下导入语句:
import icons_rc
不管qrc文件的位置如何(例如共享位置\Modules\ZA\RES\或UI位置\Modules\ZA\MDH\),此导入语句都是相同的。
只有当qrc文件的生成Python文件与表单位于相同位置时,生成的表单才能正常工作;否则会引发错误:
File "S:\...\Modules\ZA\MDH\ui_BObj.py", line 25, in <module>
import icons_rc
ModuleNotFoundError: No module named 'icons_rc'
这意味着必须将所有图像和已编译的qrc文件保存在与UI/Form文件夹相同的位置。我使用了PySide6和pyside6-rcc,我认为在PyQt中的行为也相同。
这是否意味着每个UI表单的qrc文件都必须在各自的位置创建,即使这些表单使用相同的共享图标集?
关于这个主题的所有文档/帖子都谈到了qrc文件格式和编译器,但没有提到资源文件的位置。是否不可能在一个位置创建共享/通用的图标qrc文件,然后将其编译并在不同位置的不同UI表单中使用?
英文:
When a resource file is created via Qt Designer in a Form, the python code generated by the Qt Designer includes the following import statement:
import icons_rc
This import statement is same irrespective of the qrc file location (say shared location \Modules\ZA\RES\ or ui location \Modules\ZA\MDH).
The generated form works only if the generated python file for qrc file is in same location as form; else it raises the error:
File "S:\...\Modules\ZA\MDH\ui_BObj.py", line 25, in <module>
import icons_rc
ModuleNotFoundError: No module named 'icons_rc'
This implies saving all images and compiled qrc file in same location as the UI/Form folder. I used PySide6 with pyside6-rcc and I believe this behaviour is same in PyQt as well.
Does this mean that qrc file for every UI form must be created in the respective locations, even if these forms use same common icon set?
All documentation/ posts on this topic talks about the qrc file format and compiler, but there is no indication on location of the resource files. Is it not possible to create a shared/ common icons qrc file in one location, compile it and then use it in different UI forms in different locations?
答案1
得分: 1
资源文件/ qrc 文件不需要位于 ui / form 文件夹中。如果使用正确的项目结构,就不应该出现问题。
设计师/资源/ qrc 文件都可以放在项目的顶层文件夹中:即项目的python包树之外(应该只包含python模块)。然后在生成ui模块时应该使用--from-imports
选项,这些模块可以全部放在主python包树内的一个子包中,已编译的资源模块将与它们一起放在同一个子包中。这将确保导入都能正确工作 - 只要main.py
脚本位于包的外部,即位于父文件夹中。
一旦确定了最终的项目结构,就可以在项目的顶层添加一个makefile(或等效物),它将在开发期间运行程序时执行。这个makefile可以有一个目标,调用pyuic
和pyrcc
,并提供相关的src/dest路径,这将确保设计师/资源/ qrc 文件的任何更改都会立即反映在已编译的ui/resource模块中。
一个典型的项目结构可能如下所示:
project/
designer/
mainwindow.ui
dialog.ui
icons/
logo.png
LICENSE
Makefile
main.py
prog.sh
resources.qrc
run.sh
package/
__init__.py
app.py
mainwindow.py
dialog.py
ui/
__init__.py
mainwindow_ui.py
dialog_ui.py
resources_rc.py
(prog.sh
文件是一个简单的启动程序的shell脚本,属于主要安装的一部分。run.sh
文件是只在开发期间使用的另一个shell脚本,其中包括在启动程序之前调用make
)。
PS:有关项目结构的更详细信息,请参阅此答案。
英文:
The resources files/qrc file do not need to be in the ui/form folder. If the right project structure is used, there should be no problems.
The designer/resource/qrc files can all go in the top level folder of the project: i.e. outside of the python package tree (which should only ever contain python modules). The --from-imports
option should then be used when generating the ui modules, which can all go in a sub-package within the main python package tree, with the compiled resource modules going alongside them in the same sub-package. This will ensure the imports all work correctly - so long as the main.py
script is located outside of the package, in the immediate parent folder.
Once the final project structure has been determined, a makefile (or equivalent) could also be added at the top-level of the project that will execute whenever the program is run during development. This makefile could have a target that calls pyuic
and pyrcc
with the relevant src/dest paths, which will ensure any changes to the designer/resource/qrc files are always immediately reflected in the compiled ui/resource modules.
A typical project structure might look something like this:
<!-- language: lang-none -->
project/
designer/
mainwindow.ui
dialog.ui
icons/
logo.png
LICENSE
Makefile
main.py
prog.sh
resources.qrc
run.sh
package/
__init__.py
app.py
mainwindow.py
dialog.py
ui/
__init__.py
mainwindow_ui.py
dialog_ui.py
resources_rc.py
(The prog.sh
file is a simple shell script that starts the program, and is part of the main installation. The run.sh
file is another shell script used only during development that - amongst other things - calls make
before starting the program).
PS: for a more detailed look at project structure, see this answer.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论