PySide6.QtUiTools.QUiLoader¶
- class QUiLoader¶
- The - QUiLoaderclass enables standalone applications to dynamically create user interfaces at run-time using the information stored in UI files or specified in plugin paths. 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. - In addition, you can customize or create your own user interface by deriving your own loader class. - If you have a custom component or an application that embeds Qt Widgets Designer, you can also use the QFormBuilder class provided by the QtDesigner module to create user interfaces from UI files. - The - QUiLoaderclass provides a collection of functions allowing you to create widgets based on the information stored in UI files (created with Qt Widgets Designer) or available in the specified plugin paths. The specified plugin paths can be retrieved using the- pluginPaths()function. Similarly, the contents of a UI file can be retrieved using the- load()function. For example:- def __init__(self, parent): super().__init__(parent) loader = QUiLoader() file = QFile(":/forms/myform.ui") file.open(QFile.ReadOnly) myWidget = loader.load(file, self) file.close() layout = QVBoxLayout() layout.addWidget(myWidget) setLayout(layout) - The - availableWidgets()function returns a QStringList with the class names of the widgets available in the specified plugin paths. To create these widgets, simply use the- createWidget()function. For example:- def loadCustomWidget(parent): loader = QUiLoader() myWidget = QWidget() availableWidgets = loader.availableWidgets() if availableWidgets.contains("AnalogClock"): myWidget = loader.createWidget("AnalogClock", parent) return myWidget - To make a custom widget available to the loader, you can use the - addPluginPath()function; to remove all available widgets, you can call the- clearPluginPaths()function.- The - createAction(),- createActionGroup(),- createLayout(), and- createWidget()functions are used internally by the- QUiLoaderclass whenever it has to create an action, action group, layout, or widget respectively. For that reason, you can subclass the- QUiLoaderclass and reimplement these functions to intervene the process of constructing a user interface. 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 - QFormBuilder- Creates 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.- See also - availableLayouts()¶
- Return type:
- list of strings 
 
 - Returns a list naming all available layouts that can be built using the - createLayout()function- See also - availableWidgets()¶
- Return type:
- list of strings 
 
 - Returns a list naming all available widgets that can be built using the - createWidget()function, i.e 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 - parentand- name.- The function is also used internally by the - QUiLoaderclass whenever it creates a widget. Hence, you can subclass- QUiLoaderand reimplement this function to intervene process of constructing a user interface or widget. However, in your implementation, ensure that you call- QUiLoader‘s version first.- See also - Creates a new action group with the given - parentand- name.- The function is also used internally by the - QUiLoaderclass whenever it creates a widget. Hence, you can subclass- QUiLoaderand reimplement this function to intervene process of constructing a user interface or widget. However, in your implementation, ensure that you call- QUiLoader‘s version first.- See also - createLayout(className[, parent=None[, name=""]])¶
 - Creates a new layout with the given - parentand- nameusing the class specified by- className.- The function is also used internally by the - QUiLoaderclass whenever it creates a widget. Hence, you can subclass- QUiLoaderand reimplement this function to intervene process of constructing a user interface or widget. However, in your implementation, ensure that you call- QUiLoader‘s version first.- See also - createWidget(className[, parent=None[, name=""]])¶
 - Creates a new widget with the given - parentand- nameusing 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 - QUiLoaderclass whenever it creates a widget. Hence, you can subclass- QUiLoaderand reimplement this function to intervene process of constructing a user interface or widget. However, in your implementation, ensure that you call- QUiLoader‘s version first.- See also - errorString()¶
- Return type:
- str 
 
 - Returns a human-readable description of the last error occurred in - load().- See also - isLanguageChangeEnabled()¶
- Return type:
- bool 
 
 - Returns true if dynamic retranslation on language change is enabled; returns false otherwise. - See also - isTranslationEnabled()¶
- Return type:
- bool 
 
 - load(arg__1[, parentWidget=None])¶
 - load(device[, parentWidget=None])
 - Loads a form from the given - deviceand creates a new widget with the given- parentWidgetto hold its contents.- 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’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 - 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.- See also - Returns the working directory of the loader. - See also