Provides an abstraction layer on top of the various Qt bindings (PyQt5/6 and PySide2/6).
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
UI file loading and compilation functionality that provides cross-binding compatibility for working with Qt Designer UI files. This module handles the differences between PyQt and PySide approaches to loading UI files at runtime.
Dynamically load UI files created with Qt Designer into Python widgets at runtime.
def loadUi(
uifile: str,
baseinstance=None,
workingDirectory: str | None = None
):
"""
Dynamically load a user interface from the given uifile.
Args:
uifile: String containing a file name of the UI file to load
baseinstance: If None, creates new instance of top-level widget.
Otherwise, creates UI within the given baseinstance
workingDirectory: Working directory for relative paths
Returns:
baseinstance if provided, otherwise newly created widget instance
"""Usage example:
from qtpy.uic import loadUi
from qtpy.QtWidgets import QApplication, QMainWindow
app = QApplication([])
# Load UI into a new widget instance
widget = loadUi("mainwindow.ui")
# Or load UI into an existing widget
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
loadUi("mainwindow.ui", self)
window = MainWindow()
window.show()
app.exec()Generate form classes from UI files for subclassing and advanced usage patterns.
def loadUiType(uifile: str, from_imports: bool = False):
"""
Load a .ui file and return the generated form class and Qt base class.
Available for PySide2 and PySide6. For PyQt5 and PyQt6, this function
is provided directly by the respective uic modules.
Args:
uifile: Path to the UI file to load
from_imports: Whether to use relative imports in generated code
Returns:
tuple[type, type]: (form_class, base_class) tuple where form_class
contains the UI setup methods and base_class is
the Qt widget class (QMainWindow, QDialog, etc.)
"""Usage example:
from qtpy.uic import loadUiType
from qtpy.QtWidgets import QApplication
# Generate classes from UI file
Ui_MainWindow, QtBaseClass = loadUiType("mainwindow.ui")
class MainWindow(QtBaseClass):
def __init__(self):
super().__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# Connect signals
self.ui.pushButton.clicked.connect(self.on_button_clicked)
def on_button_clicked(self):
print("Button clicked!")
app = QApplication([])
window = MainWindow()
window.show()
app.exec()Support for custom widgets defined in UI files with automatic import and registration.
class UiLoader:
"""
Subclass of QUiLoader to create user interface in a base instance.
Unlike QUiLoader itself, this class creates the user interface in an
existing instance of the top-level class if needed, mimicking the
behavior of PyQt4.uic.loadUi.
"""
def __init__(self, baseinstance, customWidgets: dict[str, type] | None = None):
"""
Create a loader for the given baseinstance.
Args:
baseinstance: Instance of the top-level class to load UI into
customWidgets: Dictionary mapping class names to class objects
for custom widgets
"""
def createWidget(self, class_name: str, parent=None, name: str = ""):
"""
Function called for each widget defined in UI file.
Args:
class_name: Name of the widget class to create
parent: Parent widget
name: Object name for the widget
Returns:
Created widget instance
"""Exceptions specific to UIC module functionality.
class NoCustomWidget(Exception):
"""
Raised when a custom widget class cannot be found during UI loading.
This exception is raised by UiLoader when a UI file references a custom
widget that is not available in the customWidgets dictionary.
"""The UIC module automatically handles differences between Qt bindings:
PyQt5/PyQt6:
PyQt5.uic.* or PyQt6.uic.*loadUi and loadUiType supportPySide2/PySide6:
loadUi implementation using QUiLoaderloadUiType implementation using compileUi (PySide2) or native support (PySide6)# Import UIC functions
from qtpy.uic import loadUi, loadUiType
# For advanced usage, access the UiLoader class
from qtpy.uic import UiLoader
# All functions work identically across PyQt5/6 and PySide2/6from qtpy.uic import loadUi, NoCustomWidget
try:
widget = loadUi("form_with_custom_widgets.ui")
except NoCustomWidget as e:
print(f"Custom widget not found: {e}")
# Handle missing custom widgetloadUi() for runtime loading or loadUiType() for class generationThis approach keeps UI design separate from code and allows for easy UI updates without code changes.
Install with Tessl CLI
npx tessl i tessl/pypi-qtpy