이 페이지에서

Qt for Python 애플리케이션에서 Qt Widgets Designer UI 파일 사용하기

폼을 Python 코드로 변환하기

시연을 위해 Qt Widgets 애니메이션 이징 예제를 사용합니다.

이 애플리케이션은 하나의 소스 파일( easing.py), UI 파일( form.ui ), 그리고 TOML 형식의 프로젝트 파일( pyproject.toml)로 구성됩니다:

[project]
name = "Easing Example"

[tool.pyside6-project]
files = ["easing.py", "form.ui"]

UI 파일은 User Interface Compiler (uic)를 사용하여 양식을 구축하는 Python 코드로 변환됩니다:

uic -g python form.ui > ui_form.py

최상위 위젯의 이름이 Form 이므로, 결과적으로 Ui_Form 라는 이름의 파이썬 클래스가 생성됩니다. 이 클래스는 위젯을 매개변수로 받는 setupUi() 함수를 제공하며, 이 함수가 호출되어 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)

이후에는 Ui_Form 클래스를 통해 위젯에 접근할 수 있습니다:

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

setupUi() 외에도, Ui_Form 는 또 다른 메서드인 retranslateUi() 를 제공하며, 이 메서드는 애플리케이션 언어의 변경을 나타내는 QEvent.LanguageChange 유형의 QEvent 에 반응하여 호출될 수 있습니다.

UiTools 방식

QUiLoader 클래스는 런타임에 사용자 인터페이스를 구성하기 위한 폼 로더 객체를 제공합니다. 이 사용자 인터페이스는 QIODevice(예: QFile 객체)에서 가져올 수 있습니다. QUiLoader::load() 함수는 파일에 포함된 사용자 인터페이스 설명을 사용하여 폼 위젯을 구성합니다.

이는 uiloader 예제를 통해 시연됩니다:

from PySide6.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.OpenModeFlag.ReadOnly):
        print(f"Cannot open {ui_file_name}: {ui_file.errorString()}")
        sys.exit(-1)
    loader = QUiLoader()
    widget = loader.load(ui_file, None)
    ui_file.close()
    if widget is None:
        print(loader.errorString())
        sys.exit(-1)
    widget.show()
    sys.exit(app.exec())

리소스 가져오기

단일 디렉터리 사용

resources.qrc와 같은 리소스 파일의 아이콘을 사용할 경우, uic 는 다음과 같은 형식의 임포트 항목을 생성합니다:

import resources_rc

이는 Resource Compiler (rcc) 도구를 호출하여( -g python 명령줄 옵션을 전달함) 생성된 resources_rc.py 파일이 폼 소스 파일과 동일한 디렉터리에 존재한다고 가정합니다.

uic --rc-prefix 명령줄 옵션을 사용하면 표시자가 앞에 추가됩니다: rc

import rc_resources

--from-imports 명령줄 옵션을 사용하면 '.'를 기준으로 임포트가 생성됩니다:

from . import resources_rc

디렉터리 트리

일부 프로젝트는 더 복잡한 디렉터리 구조를 가지고 있습니다. 예를 들어:

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

이 경우 리소스 파일은 폼 소스 파일과 같은 디렉터리에 있지 않으며, ` .ui ` 파일에는 일반적으로 리소스 파일에 대한 상대 경로가 포함됩니다:

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

이 경우, 명령줄 옵션 ` --absolute-imports `을 사용하여 Python에서 절대 임포트를 생성할 수 있으며, 그 결과는 다음과 같습니다:

import resources.resources_rc

이는 ..가 Python 임포트 경로 목록에 포함된 프로젝트의 루트 디렉터리라는 가정을 바탕으로 합니다.

더 깊게 중첩된 디렉터리 구조의 경우, 명령줄 옵션 ` --python-paths <path list> `을 사용하여 Python 임포트 경로 목록을 전달할 수 있습니다. 그러면 uic 는 폼 파일 경로를 경로 구성 요소와 대조하여 프로젝트 루트를 파악하려고 시도합니다.

--python-paths 가 지정되지 않은 경우, 기본적으로 PYTHONPATH 환경 변수가 확인됩니다.

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.