The QUiLoader class enables standalone applications to dynamically create user interfaces at run-time using the information stored in UI files or specified in plugin paths. 更多...
头: | #include <QUiLoader> |
qmake: | QT += uitools |
继承: | QObject |
QUiLoader (QObject * parent = nullptr) | |
virtual | ~QUiLoader () override |
void | addPluginPath (const QString & path ) |
QStringList | availableLayouts () const |
QStringList | availableWidgets () const |
void | clearPluginPaths () |
virtual QAction * | createAction (QObject * parent = nullptr, const QString & name = QString()) |
virtual QActionGroup * | createActionGroup (QObject * parent = nullptr, const QString & name = QString()) |
virtual QLayout * | createLayout (const QString & className , QObject * parent = nullptr, const QString & name = QString()) |
virtual QWidget * | createWidget (const QString & className , QWidget * parent = nullptr, const QString & name = QString()) |
QString | errorString () const |
bool | isLanguageChangeEnabled () const |
QWidget * | load (QIODevice * device , QWidget * parentWidget = nullptr) |
QStringList | pluginPaths () const |
void | setLanguageChangeEnabled (bool enabled ) |
void | setWorkingDirectory (const QDir & dir ) |
QDir | workingDirectory () const |
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 () 函数。例如:
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); }
By including the user interface in the form's resources (
myform.qrc
), we ensure that it will be present at run-time:
<!DOCTYPE RCC><RCC version="1.0"> <qresource prefix="/forms"> <file>myform.ui</file> </qresource> </RCC>
The availableWidgets () 函数返回 QStringList with the class names of the widgets available in the specified plugin paths. To create these widgets, simply use the createWidget () 函数。例如:
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 () 函数。
The createAction (), createActionGroup (), createLayout (),和 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 计算器构建者范例 .
另请参阅 Qt UI Tools and QFormBuilder .
Creates a form loader with the given parent .
[override virtual]
QUiLoader::
~QUiLoader
()
Destroys the loader.
添加给定 path to the list of paths in which the loader will search when locating plugins.
另请参阅 pluginPaths () 和 clearPluginPaths ().
Returns a list naming all available layouts that can be built using the createLayout () 函数
该函数在 Qt 4.5 引入。
另请参阅 createLayout ().
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.
另请参阅 pluginPaths () 和 createWidget ().
Clears the list of paths in which the loader will search when locating plugins.
另请参阅 addPluginPath () 和 pluginPaths ().
[虚拟]
QAction
*QUiLoader::
createAction
(
QObject
*
parent
= nullptr, const
QString
&
name
= QString())
创建新的动作采用给定 parent and name .
The function is also used internally by the QUiLoader class whenever it creates a widget. Hence, you can subclass QUiLoader and 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.
另请参阅 createActionGroup (), createWidget (),和 load ().
[虚拟]
QActionGroup
*QUiLoader::
createActionGroup
(
QObject
*
parent
= nullptr, const
QString
&
name
= QString())
Creates a new action group with the given parent and name .
The function is also used internally by the QUiLoader class whenever it creates a widget. Hence, you can subclass QUiLoader and 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.
另请参阅 createAction (), createWidget (),和 load ().
[虚拟]
QLayout
*QUiLoader::
createLayout
(const
QString
&
className
,
QObject
*
parent
= nullptr, const
QString
&
name
= QString())
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 widget. Hence, you can subclass QUiLoader and 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.
另请参阅 createWidget () 和 load ().
[虚拟]
QWidget
*QUiLoader::
createWidget
(const
QString
&
className
,
QWidget
*
parent
= nullptr, const
QString
&
name
= QString())
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 () 函数。
The function is also used internally by the QUiLoader class whenever it creates a widget. Hence, you can subclass QUiLoader and 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.
另请参阅 availableWidgets () 和 load ().
Returns a human-readable description of the last error occurred in load ().
该函数在 Qt 5.0 引入。
另请参阅 load ().
Returns true if dynamic retranslation on language change is enabled; returns false otherwise.
该函数在 Qt 4.5 引入。
另请参阅 setLanguageChangeEnabled ().
Loads a form from the given device and creates a new widget with the given parentWidget to hold its contents.
另请参阅 createWidget () 和 errorString ().
Returns a list naming the paths in which the loader will search when locating custom widget plugins.
另请参阅 addPluginPath () 和 clearPluginPaths ().
若 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.
该函数在 Qt 4.5 引入。
另请参阅 isLanguageChangeEnabled ().
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.
另请参阅 workingDirectory ().
Returns the working directory of the loader.
另请参阅 setWorkingDirectory ().