QUiLoader Class▲
-
Header: QUiLoader
-
Since: Qt 4.1
-
qmake: QT += uitools
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 the pluginPaths() function. Similarly, the contents of a UI file can be retrieved using the load() function. For example:
MyWidget::
MyWidget(QWidget *
parent)
:
QWidget(parent)
{
QUiLoader loader;
QFile file(":/forms/myform.ui"
);
file.open(QFile::
ReadOnly);
QWidget *
myWidget =
loader.load(&
amp;file, this
);
file.close();
QVBoxLayout *
layout =
new
QVBoxLayout;
layout-&
gt;addWidget(myWidget);
setLayout(layout);
}
By including the user interface in the form's resources (myform.qrc), we ensure that it will be present at run-time:
&
lt;!
DOCTYPE RCC&
gt;&
lt;RCC version=
"1.0"
&
gt;
&
lt;qresource prefix=
"/forms"
&
gt;
&
lt;file&
gt;myform.ui&
lt;/
file&
gt;
&
lt;/
qresource&
gt;
&
lt;/
RCC&
gt;
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:
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 the clearPluginPaths() function.
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. For that reason, you can subclass the QUiLoader 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▲
See also Qt UI Tools, QFormBuilder