在 Qt for Python 应用程序使用 Designer UI 文件

将表单转换成 Python 代码

为演示,我们使用 Qt Widgets 动画缓和范例。

应用程序的组成由一源文件, easing.py ,UI 文件 form.ui ,资源文件 easing.qrc 和工程文件, easing.pyproject 文件按 YAML 格式:

{
    "files": ["easing.qrc", "ui_form.py", "easing.py", "easing_rc.py",
              "form.ui"]
}
					

要将 UI 文件转换成 Python 代码,构建表单使用 uic (用户界面编译器) :

uic -g python form.ui > ui_form.py
					

由于顶层 Widget 名为 表单 ,这产生的 Python 类名为 Ui_Form 被生成。它提供函数 setupUi() ,将 Widget 作为参数,调用它以创建 UI 元素:

from ui_form import Ui_Form
...
class Window(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        self.m_ui = Ui_Form()
        self.m_ui.setupUi(self)
					

稍后,可以访问 Widget 凭借 Ui_Form 类:

self.m_ui.graphicsView.setScene(self.m_scene)
					

除了 setupUi() , Ui_Form 提供另一方法 retranslateUi() ,可以被调用以反应 QEvent 类型 QEvent .LanguageChange,指示应用程序语言的改变。

UiTools 方式

The QUiLoader 类提供表单加载器对象以在运行时构造用户界面。此用户界面可以检索自任何 QIODevice ,如 QFile 对象。 QUiLoader::load () 函数使用文件中包含的用户界面描述构造表单 Widget。

通过 uiloader 范例来演示它:

from PySide2.QtUiTools import QUiLoader
if __name__ == '__main__':
    # Some code to obtain the form file name, ui_file_name
    app = QApplication(sys.argv)
    ui_file = QFile(ui_file_name)
    if not ui_file.open(QIODevice.ReadOnly):
        print("Cannot open {}: {}".format(ui_file_name, ui_file.errorString()))
        sys.exit(-1)
    loader = QUiLoader()
    widget = loader.load(ui_file, None)
    ui_file.close()
    if not widget:
        print(loader.errorString())
        sys.exit(-1)
    widget.show()
    sys.exit(app.exec_())
					

Resource imports

Single directory usage

When using icons from resource files , say resources.qrc , uic will generate an import of the form:

import resources_rc
					

This assumes that a file resources_rc.py generated by calling the RCC (资源编译器) tool (passing the -g python command line option) exists in the same directory as the form source.

uic has a command line option --rc-prefix causing the rc indicator to be prepended:

import rc_resources
					

The command line option --from-imports causes the imports to be generated relative to '.':

from . import resources_rc
					

Directory trees

Some projects have more complicated directory trees, for example:

project
    resources   (resources.qrc)
    ui          (.ui files)
					

The resource file is then not in the same directory as the form source and the .ui files typically have relative paths to the resource files:

<include location="../resources/resources.qrc"/>
					

In this case, the command line option --absolute-imports can be used to generate an absolute import in Python, resulting in:

import resources.resources_rc
					

based on the assumption that . . is the root directory of the project contained in the Python import path list.

For more deeply nested trees, it is possible to use the command line option --python-paths <path list> to pass a Python import path list. uic will then try to determine the project root by matching the form file path against the path components.

--python-paths is not given, the environment variable PYTHONPATH is by default checked.