QUiLoader¶
Synopsis¶
Functions¶
def
addPluginPath
(path)def
availableLayouts
()def
availableWidgets
()def
clearPluginPaths
()def
errorString
()def
isLanguageChangeEnabled
()def
isTranslationEnabled
()def
load
(arg__1[, parentWidget=None])def
load
(device[, parentWidget=None])def
pluginPaths
()def
registerCustomWidget
(customWidgetType)def
setLanguageChangeEnabled
(enabled)def
setTranslationEnabled
(enabled)def
setWorkingDirectory
(dir)def
workingDirectory
()
Virtual functions¶
def
createAction
([parent=None[, name=””]])def
createActionGroup
([parent=None[, name=””]])def
createLayout
(className[, parent=None[, name=””]])def
createWidget
(className[, parent=None[, name=””]])
Detailed Description¶
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 Designer , you can also use the QFormBuilder class provided by the QtDesigner module to create user interfaces from UI files.
The
QUiLoader
class provides a collection of functions allowing you to create widgets based on the information stored in UI files (created with Qt Designer ) or available in the specified plugin paths. The specified plugin paths can be retrieved using thepluginPaths()
function. Similarly, the contents of a UI file can be retrieved using theload()
function. For example:MyWidget::MyWidget(QWidget *parent) : QWidget(parent) { QUiLoader loader; QFile file(":/forms/myform.ui"); file.open(QFile::ReadOnly); QWidget *myWidget = loader.load(&file, this); file.close(); QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(myWidget); setLayout(layout); }The
availableWidgets()
function returns aQStringList
with the class names of the widgets available in the specified plugin paths. To create these widgets, simply use thecreateWidget()
function. For example:QWidget *loadCustomWidget(QWidget *parent) { QUiLoader loader; QWidget *myWidget; QStringList 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 theclearPluginPaths()
function.The
createAction()
,createActionGroup()
,createLayout()
, andcreateWidget()
functions are used internally by theQUiLoader
class whenever it has to create an action, action group, layout, or widget respectively. For that reason, you can subclass theQUiLoader
class 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
QUiLoader
class, see the Calculator Builder Example.See also
Qt UI Tools
QFormBuilder
- class PySide2.QtUiTools.QUiLoader([parent=None])¶
- param parent:
Creates a form loader with the given
parent
.
- PySide2.QtUiTools.QUiLoader.addPluginPath(path)¶
- Parameters:
path – str
Adds the given
path
to the list of paths in which the loader will search when locating plugins.See also
- PySide2.QtUiTools.QUiLoader.availableLayouts()¶
- Return type:
list of strings
Returns a list naming all available layouts that can be built using the
createLayout()
functionSee also
- PySide2.QtUiTools.QUiLoader.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
- PySide2.QtUiTools.QUiLoader.clearPluginPaths()¶
Clears the list of paths in which the loader will search when locating plugins.
See also
- PySide2.QtUiTools.QUiLoader.createAction([parent=None[, name=""]])¶
- Parameters:
parent –
PySide2.QtCore.QObject
name – str
- Return type:
Creates a new action with the given
parent
andname
.The function is also used internally by the
QUiLoader
class whenever it creates a widget. Hence, you can subclassQUiLoader
and reimplement this function to intervene process of constructing a user interface or widget. However, in your implementation, ensure that you callQUiLoader
‘s version first.See also
- PySide2.QtUiTools.QUiLoader.createActionGroup([parent=None[, name=""]])¶
- Parameters:
parent –
PySide2.QtCore.QObject
name – str
- Return type:
Creates a new action group with the given
parent
andname
.The function is also used internally by the
QUiLoader
class whenever it creates a widget. Hence, you can subclassQUiLoader
and reimplement this function to intervene process of constructing a user interface or widget. However, in your implementation, ensure that you callQUiLoader
‘s version first.See also
- PySide2.QtUiTools.QUiLoader.createLayout(className[, parent=None[, name=""]])¶
- Parameters:
className – str
parent –
PySide2.QtCore.QObject
name – str
- Return type:
Creates a new layout with the given
parent
andname
using the class specified byclassName
.The function is also used internally by the
QUiLoader
class whenever it creates a widget. Hence, you can subclassQUiLoader
and reimplement this function to intervene process of constructing a user interface or widget. However, in your implementation, ensure that you callQUiLoader
‘s version first.See also
- PySide2.QtUiTools.QUiLoader.createWidget(className[, parent=None[, name=""]])¶
- Parameters:
className – str
parent –
PySide2.QtWidgets.QWidget
name – str
- Return type:
Creates a new widget with the given
parent
andname
using 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
QUiLoader
class whenever it creates a widget. Hence, you can subclassQUiLoader
and reimplement this function to intervene process of constructing a user interface or widget. However, in your implementation, ensure that you callQUiLoader
‘s version first.See also
- PySide2.QtUiTools.QUiLoader.errorString()¶
- Return type:
str
Returns a human-readable description of the last error occurred in
load()
.See also
- PySide2.QtUiTools.QUiLoader.isLanguageChangeEnabled()¶
- Return type:
bool
Returns true if dynamic retranslation on language change is enabled; returns false otherwise.
See also
- PySide2.QtUiTools.QUiLoader.isTranslationEnabled()¶
- Return type:
bool
Returns true if translation is enabled; returns false otherwise.
See also
- PySide2.QtUiTools.QUiLoader.load(device[, parentWidget=None])¶
- Parameters:
device –
PySide2.QtCore.QIODevice
parentWidget –
PySide2.QtWidgets.QWidget
- Return type:
Loads a form from the given
device
and creates a new widget with the givenparentWidget
to hold its contents.See also
- PySide2.QtUiTools.QUiLoader.load(arg__1[, parentWidget=None])
- Parameters:
arg__1 – str
parentWidget –
PySide2.QtWidgets.QWidget
- Return type:
- PySide2.QtUiTools.QUiLoader.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
- PySide2.QtUiTools.QUiLoader.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
’spaintEvent()
method.class Circle(QWidget): def paintEvent(self, event): painter = QPainter(self) 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() # ...
- PySide2.QtUiTools.QUiLoader.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.See also
- PySide2.QtUiTools.QUiLoader.setTranslationEnabled(enabled)¶
- Parameters:
enabled – bool
If
enabled
is true, user interfaces loaded by this loader will be translated. Otherwise, the user interfaces will not be translated.Note
This is orthogonal to languageChangeEnabled.
- PySide2.QtUiTools.QUiLoader.setWorkingDirectory(dir)¶
- Parameters:
dir –
PySide2.QtCore.QDir
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
- PySide2.QtUiTools.QUiLoader.workingDirectory()¶
- Return type:
Returns the working directory of the loader.
See also
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.