CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-qtpy

Provides an abstraction layer on top of the various Qt bindings (PyQt5/6 and PySide2/6).

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

uic-module.mddocs/

UIC Module

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.

Capabilities

UI File Loading

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()

UI Type Loading

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()

Custom Widget Support

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
        """

Exception Classes

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

Cross-Binding Compatibility

The UIC module automatically handles differences between Qt bindings:

PyQt5/PyQt6:

  • Direct import from PyQt5.uic.* or PyQt6.uic.*
  • Native loadUi and loadUiType support

PySide2/PySide6:

  • Custom loadUi implementation using QUiLoader
  • Custom loadUiType implementation using compileUi (PySide2) or native support (PySide6)
  • Automatic custom widget discovery and loading
  • Workarounds for PySide-specific issues

Import Patterns

# 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/6

Error Handling

from 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 widget

UI File Workflow

  1. Design: Create UI files using Qt Designer
  2. Load: Use loadUi() for runtime loading or loadUiType() for class generation
  3. Connect: Connect signals and implement business logic
  4. Deploy: Ship UI files alongside Python code

This 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

docs

compatibility.md

core-package.md

development-tools.md

index.md

qt-modules.md

uic-module.md

tile.json