Python

Building a UI with Qt Designer and PySide

Design the window visually in Qt Designer, then load the .ui straight from Python.

sf
Naoki « segfault »
systèmes & back-end · updated Jan 2025
This guide revisits and updates an original tutorial from noiretaya.com (log.noiretaya.com/259). The code has been refreshed for current versions.

Design first, code second

Qt Designer lets you lay out widgets visually and saves a .ui XML file. With PySide you can load that file at runtime — no code generation step needed.

Load the .ui at runtime

import sys
from PySide6.QtWidgets import QApplication
from PySide6.QtUiTools import QUiLoader
from PySide6.QtCore import QFile

app = QApplication(sys.argv)
loader = QUiLoader()
ui_file = QFile("form.ui")
ui_file.open(QFile.ReadOnly)
window = loader.load(ui_file)
ui_file.close()

window.show()
sys.exit(app.exec())

Wiring up widgets

Widgets keep the objectName you set in Designer, so you can find them and connect signals.

btn = window.findChild(QPushButton, "okButton")
btn.clicked.connect(lambda: print("clicked"))
Two approaches: load the .ui at runtime (shown here) for quick iteration, or pre-compile it with pyside6-uic form.ui -o form_ui.py and subclass it for larger apps.