PySide6.QtUiTools.QUiLoader¶
- class QUiLoader¶
Loads and instantiates Qt Widgets Designer forms at runtime. More…
Synopsis¶
Methods¶
def
__init__()def
addPluginPath()def
errorString()def
load()def
pluginPaths()
Virtual methods¶
def
createAction()def
createLayout()def
createWidget()
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
QUiLoaderto 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 theerrorString()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
QUiLoaderand override thecreateWidget()function. If this is not possible, you can also let the module load Qt Widgets Designer plugins by adding their locations viaaddPluginPath()or theQT_PLUGIN_PATHenvironment 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 thecreateWidget()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(), andcreateWidget()functions are used internally by theQUiLoaderclass whenever it has to create an action, action group, layout, or widget respectively. You can subclassQUiLoaderand 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
QUiLoaderclass, see the Calculator Builder.See also
Qt-UI-Tools
QFormBuilderCreates a form loader with the given
parent.- addPluginPath(path)¶
- Parameters:
path – str
Adds the given
pathto 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.
See also
- availableLayouts()¶
- Return type:
list of strings
Returns a list of all available layouts that can be built using the
createLayout()function.See also
- 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.See also
- clearPluginPaths()¶
Clears the list of paths in which the loader will search when locating plugins.
See also
- createAction([parent=None[, name=""]])¶
Creates a new action with the given
parentandname.The function is also used internally by the
QUiLoaderclass whenever it creates an action. Therefore, you can subclassQUiLoaderand reimplement this function to intervene in the process of constructing a user interface or widget. However, in your implementation, ensure that you callQUiLoader‘s version first.See also
Creates a new action group with the given
parentandname.The function is also used internally by the
QUiLoaderclass whenever it creates an action group. Therefore, you can subclassQUiLoaderand reimplement this function to intervene in the process of constructing a user interface or widget. However, in your implementation, ensure that you callQUiLoader‘s version first.See also
- createLayout(className[, parent=None[, name=""]])¶
Creates a new layout with the given
parentandnameusing the class specified byclassName.The function is also used internally by the
QUiLoaderclass whenever it creates a layout. Therefore, you can subclassQUiLoaderand reimplement this function to intervene in the process of constructing a user interface or widget. However, in your implementation, ensure that you callQUiLoader‘s version first.See also
- createWidget(className[, parent=None[, name=""]])¶
Creates a new widget with the given
parentandnameusing the class specified byclassName. You can use this function to create any of the widgets returned by theavailableWidgets()function.The function is also used internally by the
QUiLoaderclass whenever it creates a widget. Therefore, you can subclassQUiLoaderand reimplement this function to intervene in the process of constructing a user interface or widget. However, in your implementation, ensure that you callQUiLoader‘s version first.See also
- errorString()¶
- Return type:
str
Returns a human-readable description of the last error that occurred in
load().See also
- isLanguageChangeEnabled()¶
- Return type:
bool
Returns
trueif dynamic retranslation on language change is enabled; returnsfalseotherwise.The default is
false.See also
- isTranslationEnabled()¶
- Return type:
bool
- load(arg__1[, parentWidget=None])¶
- load(device[, parentWidget=None])
Instantiates a form from the given
device. Returns a new QWidget with the givenparentWidgetif successful. Returnsnullptrotherwise.Warning
Only load forms from trusted sources, like the Qt resource system. Loading
.uifiles 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.See also
- pluginPaths()¶
- Return type:
list of strings
Returns a list naming the paths in which the loader will search when locating custom widget plugins.
See also
- 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
customWidgetTypeargument. 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’spaintEvent()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
enabledis 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.See also
- setTranslationEnabled(enabled)¶
- Parameters:
enabled – bool
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.
See also
Returns the working directory of the loader.
See also