PySide6.QtUiTools.QUiLoader

class QUiLoader

Loads and instantiates Qt Widgets Designer forms at runtime. More

Inheritance diagram of PySide6.QtUiTools.QUiLoader

Synopsis

Methods

Virtual methods

Note

This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE

Detailed Description

Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

Use QUiLoader to dynamically create QWidget-based user interfaces based on the information stored in UI files (created with Qt Widgets Designer).

The load() function reads the content of a UI file, instantiates the widgets described in the file, and returns a pointer to the top-level QWidget. This widget can then be shown:

def __init__(self, parent):
    super().__init__(parent)

    file = QFile(":/forms/myform.ui")
    if not file.open(QFile.OpenModeFlag.ReadOnly):
        qFatal("Cannot open resource file")
    loader = QUiLoader()
    myWidget = loader.load(file, self)
    layout = QVBoxLayout()
    layout.addWidget(myWidget)
    setLayout(layout)

If the instantiation fails, the function returns a nullptr; use the errorString() function to retrieve a human-readable description of the error that occurred.

If the UI file contains custom widgets implemented in a Qt Widgets Designer plugin, loading will fail by default. To work around this, you can subclass QUiLoader and override the createWidget() function. If this is not possible, you can also let the module load Qt Widgets Designer plugins by adding their locations via addPluginPath() or the QT_PLUGIN_PATH environment variable. See the Creating Custom Widgets for Qt Widgets Designer page for more details.

You can load a specific widget from a UI file, instead of a complete UI file. Use the availableWidgets() function to retrieve the names of the available widgets, and the createWidget() function to instantiate a specific one. For example:

def loadCustomWidget(className,parent):

    loader = QUiLoader()
    availableWidgets = loader.availableWidgets()
    if not availableWidgets.contains(className):
        qWarning() << "Cannot create widget" << className
        return None

    return loader.createWidget(className, parent)

The createAction() , createActionGroup() , createLayout() , and createWidget() functions are used internally by the QUiLoader class whenever it has to create an action, action group, layout, or widget respectively. You can subclass QUiLoader and reimplement these functions to customize the UI creation workflow. For example, you might want to have a list of the actions created when loading a form or creating a custom widget.

For a complete example using the QUiLoader class, see the Calculator Builder.

See also

Qt-UI-Tools QFormBuilder

__init__([parent=None])
Parameters:

parentQObject

Creates a form loader with the given parent.

addPluginPath(path)
Parameters:

path – str

Adds the given path to the list of paths in which the loader will search when locating plugins.

Warning

Only set paths that you trust. Allowing untrusted users to create or add content in a specified path may lead to security vulnerabilities.

availableLayouts()
Return type:

list of strings

Returns a list of all available layouts that can be built using the createLayout() function.

See also

createLayout()

availableWidgets()
Return type:

list of strings

Returns a list of all available widgets that can be built using the createWidget() function, that is, all the widgets specified within the given plugin paths.

clearPluginPaths()

Clears the list of paths in which the loader will search when locating plugins.

createAction([parent=None[, name=""]])
Parameters:
Return type:

QAction

Creates a new action with the given parent and name.

The function is also used internally by the QUiLoader class whenever it creates an action. Therefore, you can subclass QUiLoader and reimplement this function to intervene in the process of constructing a user interface or widget. However, in your implementation, ensure that you call QUiLoader ‘s version first.

createActionGroup([parent=None[, name=""]])
Parameters:
Return type:

QActionGroup

Creates a new action group with the given parent and name.

The function is also used internally by the QUiLoader class whenever it creates an action group. Therefore, you can subclass QUiLoader and reimplement this function to intervene in the process of constructing a user interface or widget. However, in your implementation, ensure that you call QUiLoader ‘s version first.

createLayout(className[, parent=None[, name=""]])
Parameters:
  • className – str

  • parentQObject

  • name – str

Return type:

QLayout

Creates a new layout with the given parent and name using the class specified by className.

The function is also used internally by the QUiLoader class whenever it creates a layout. Therefore, you can subclass QUiLoader and reimplement this function to intervene in the process of constructing a user interface or widget. However, in your implementation, ensure that you call QUiLoader ‘s version first.

createWidget(className[, parent=None[, name=""]])
Parameters:
  • className – str

  • parentQWidget

  • name – str

Return type:

QWidget

Creates a new widget with the given parent and name using the class specified by className. You can use this function to create any of the widgets returned by the availableWidgets() function.

The function is also used internally by the QUiLoader class whenever it creates a widget. Therefore, you can subclass QUiLoader and reimplement this function to intervene in the process of constructing a user interface or widget. However, in your implementation, ensure that you call QUiLoader ‘s version first.

errorString()
Return type:

str

Returns a human-readable description of the last error that occurred in load() .

See also

load()

isLanguageChangeEnabled()
Return type:

bool

Returns true if dynamic retranslation on language change is enabled; returns false otherwise.

The default is false.

isTranslationEnabled()
Return type:

bool

load(arg__1[, parentWidget=None])
Parameters:
  • arg__1PyPathLike

  • parentWidgetQWidget

Return type:

QWidget

load(device[, parentWidget=None])
Parameters:
Return type:

QWidget

Instantiates a form from the given device. Returns a new QWidget with the given parentWidget if successful. Returns nullptr otherwise.

Warning

Only load forms from trusted sources, like the Qt resource system. Loading .ui files from untrusted sources can lead to security threats in your application, such as denial of service attacks, UI deception, or the loading of unexpected plugins.

pluginPaths()
Return type:

list of strings

Returns a list naming the paths in which the loader will search when locating custom widget plugins.

registerCustomWidget(customWidgetType)
Parameters:

customWidgetType – object

Registers a Python created custom widget to QUiLoader, so it can be recognized when loading a .ui file. The custom widget type is passed via the customWidgetType argument. This is needed when you want to override a virtual method of some widget in the interface, since duck punching will not work with widgets created by QUiLoader based on the contents of the .ui file.

(Remember that duck punching virtual methods is an invitation for your own demise!)

Let’s see an obvious example. If you want to create a new widget it’s probable you’ll end up overriding QWidget’s paintEvent() method.

class Circle(QWidget):
    def paintEvent(self, event):
        with QPainter(self) as painter:
            painter.setPen(self.pen)
            painter.setBrush(QBrush(self.color))
            painter.drawEllipse(event.rect().center(), 20, 20)

# ...

loader = QUiLoader()
loader.registerCustomWidget(Circle)
circle = loader.load('circle.ui')
circle.show()

# ...
setLanguageChangeEnabled(enabled)
Parameters:

enabled – bool

If enabled is true, user interfaces loaded by this loader will automatically retranslate themselves upon receiving a language change event. Otherwise, the user interfaces will not be retranslated.

setTranslationEnabled(enabled)
Parameters:

enabled – bool

setWorkingDirectory(dir)
Parameters:

dirQDir

Sets the working directory of the loader to dir. The loader will look for other resources, such as icons and resource files, in paths relative to this directory.

Warning

Only set a directory that you trust. Allowing untrusted users to create or add content in the working directory may lead to security vulnerabilities.

workingDirectory()
Return type:

QDir

Returns the working directory of the loader.