在 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.