QQmlApplicationEngine 类

QQmlApplicationEngine provides a convenient way to load an application from a single QML file. 更多...

头: #include <QQmlApplicationEngine>
qmake: QT += qml
Since: Qt 5.1
继承: QQmlEngine

公共函数

QQmlApplicationEngine (QObject * parent = Q_NULLPTR)
QQmlApplicationEngine (const QUrl & url , QObject * parent = Q_NULLPTR)
QQmlApplicationEngine (const QString & filePath , QObject * parent = Q_NULLPTR)
~QQmlApplicationEngine ()
QList<QObject *> rootObjects () const

公共槽

void load (const QUrl & url )
void load (const QString & filePath )
void loadData (const QByteArray & data , const QUrl & url = QUrl())

信号

void objectCreated (QObject * object , const QUrl & url )

额外继承成员

详细描述

QQmlApplicationEngine provides a convenient way to load an application from a single QML file.

此类组合 QQmlEngine and QQmlComponent 提供加载单个 QML 文件的便捷方式。它还向 QML 暴露某些中心应用程序功能,C++/QML 混合应用程序通常从 C++ 进行控制。

它可以像这样使用:

#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine("main.qml");
    return app.exec();
}
					

不像 QQuickView , QQmlApplicationEngine does not automatically create a root window. If you are using visual items from Qt Quick, you will need to place them inside of a Window .

还可以使用 QCoreApplication with QQmlApplicationEngine , if you are not using any QML modules which require a QGuiApplication (譬如 QtQuick ).

配置列表的改变从默认 QQmlEngine :

  • 连接 Qt. quit () 到 QCoreApplication::quit ()
  • 自动从主 QML 文件相邻 i18n 目录下加载翻译文件。
  • 自动设置孵化控制器,若场景包含 QQuickWindow .
  • 自动设置 QQmlFileSelector 作为 URL 拦截器,将文件选择器应用于所有 QML 文件和资产。

可以进一步微调引擎行为,通过使用继承的方法从 QQmlEngine .

成员函数文档编制

QQmlApplicationEngine:: QQmlApplicationEngine ( QObject * parent = Q_NULLPTR)

Create a new QQmlApplicationEngine 采用给定 parent . You will have to call load () later in order to load a QML file.

QQmlApplicationEngine:: QQmlApplicationEngine (const QUrl & url , QObject * parent = Q_NULLPTR)

Create a new QQmlApplicationEngine and loads the QML file at the given url . This is provided as a convenience, and is the same as using the empty constructor and calling load afterwards.

QQmlApplicationEngine:: QQmlApplicationEngine (const QString & filePath , QObject * parent = Q_NULLPTR)

Create a new QQmlApplicationEngine and loads the QML file at the given filePath , which must be a local file path. If a relative path is given then it will be interpreted as relative to the working directory of the application.

This is provided as a convenience, and is the same as using the empty constructor and calling load afterwards.

QQmlApplicationEngine:: ~QQmlApplicationEngine ()

销毁 QQmlApplicationEngine and all QML objects it loaded.

[slot] void QQmlApplicationEngine:: load (const QUrl & url )

Loads the root QML file located at url . The object tree defined by the file is created immediately for local file urls. Remote urls are loaded asynchronously, listen to the objectCreated signal to determine when the object tree is ready.

若发生错误,将打印错误消息采用 qWarning .

[slot] void QQmlApplicationEngine:: load (const QString & filePath )

Loads the root QML file located at filePath . filePath must be a path to a local file. If filePath is a relative path, it is taken as relative to the application's working directory. The object tree defined by the file is instantiated immediately.

若发生错误,将打印错误消息采用 qWarning .

[slot] void QQmlApplicationEngine:: loadData (const QByteArray & data , const QUrl & url = QUrl())

Loads the QML given in data . The object tree defined by data is instantiated immediately.

url is specified it is used as the base url of the component. This affects relative paths within the data and error messages.

若发生错误,将打印错误消息采用 qWarning .

[signal] void QQmlApplicationEngine:: objectCreated ( QObject * object , const QUrl & url )

This signal is emitted when an object finishes loading. If loading was successful, object contains a pointer to the loaded object, otherwise the pointer is NULL.

url to the component the object came from is also provided.

注意: If the path to the component was provided as a QString containing a relative path, the url will contain a fully resolved path to the file.

QList < QObject *> QQmlApplicationEngine:: rootObjects () const

Returns a list of all the root objects instantiated by the QQmlApplicationEngine . This will only contain objects loaded via load () or a convenience constructor.

注意: In Qt versions prior to 5.9, this function is marked as non- const .