QQmlApplicationEngine Class

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

Header: #include <QQmlApplicationEngine>
qmake: QT += qml
Since: Qt 5.1
Inherits: QQmlEngine

This class was introduced in Qt 5.1.

Public Functions

QQmlApplicationEngine(const QString &filePath, QObject *parent = nullptr)
QQmlApplicationEngine(const QUrl &url, QObject *parent = nullptr)
QQmlApplicationEngine(QObject *parent = nullptr)
virtual ~QQmlApplicationEngine() override
QList<QObject *> rootObjects() const
  • 26 public functions inherited from QQmlEngine
  • 17 public functions inherited from QJSEngine
  • 30 public functions inherited from QObject

Public Slots

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

Signals

void objectCreated(QObject *object, const QUrl &url)
typedef QObjectList
QList<T> qFindChildren(const QObject *obj, const QRegExp &regExp)
QJSEngine *qjsEngine(const QObject *object)
QObject *qmlAttachedPropertiesObject(const QObject *attachee, bool create = ...)
void qmlClearTypeRegistrations()
QQmlContext *qmlContext(const QObject *object)
QQmlInfo qmlDebug(const QObject *object)
QQmlEngine *qmlEngine(const QObject *object)
QQmlInfo qmlInfo(const QObject *object)
bool qmlProtectModule(const char *uri, int majVersion)
int qmlRegisterExtendedType(const char *uri, int versionMajor, int versionMinor, const char *qmlName)
int qmlRegisterExtendedUncreatableType(const char *uri, int versionMajor, int versionMinor, const char *qmlName, const QString &reason)
int qmlRegisterInterface(const char *typeName)
void qmlRegisterModule(const char *uri, int versionMajor, int versionMinor)
int qmlRegisterRevision(const char *uri, int versionMajor, int versionMinor)
int qmlRegisterSingletonType(const char *uri, int versionMajor, int versionMinor, const char *typeName, QJSValue (*)(QQmlEngine *, QJSEngine *) callback)
int qmlRegisterSingletonType(const char *uri, int versionMajor, int versionMinor, const char *typeName, QObject *(*)(QQmlEngine *, QJSEngine *) callback)
int qmlRegisterSingletonType(const QUrl &url, const char *uri, int versionMajor, int versionMinor, const char *qmlName)
int qmlRegisterType(const char *uri, int versionMajor, int versionMinor, const char *qmlName)
int qmlRegisterType()
int qmlRegisterType(const QUrl &url, const char *uri, int versionMajor, int versionMinor, const char *qmlName)
int qmlRegisterTypeNotAvailable(const char *uri, int versionMajor, int versionMinor, const char *qmlName, const QString &message)
int qmlRegisterUncreatableMetaObject(const QMetaObject &staticMetaObject, const char *uri, int versionMajor, int versionMinor, const char *qmlName, const QString &reason)
int qmlRegisterUncreatableType(const char *uri, int versionMajor, int versionMinor, const char *qmlName, const QString &message)
int qmlTypeId(const char *uri, int versionMajor, int versionMinor, const char *qmlName)
QQmlInfo qmlWarning(const QObject *object)
T qobject_cast(QObject *object)
T qobject_cast(const QObject *object)

Macros

Additional Inherited Members

  • 1 property inherited from QQmlEngine
  • 1 property inherited from QObject
  • 4 static public members inherited from QQmlEngine
  • 9 static public members inherited from QObject
  • 1 protected function inherited from QQmlEngine
  • 9 protected functions inherited from QObject

Detailed Description

This class combines a QQmlEngine and QQmlComponent to provide a convenient way to load a single QML file. It also exposes some central application functionality to QML, which a C++/QML hybrid application would normally control from C++.

It can be used like so:

#include <QGuiApplication>
#include <QQmlApplicationEngine>

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

Unlike 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.

You can also use QCoreApplication with QQmlApplicationEngine, if you are not using any QML modules which require a QGuiApplication (such as QtQuick).

List of configuration changes from a default QQmlEngine:

  • Connecting Qt.quit() to QCoreApplication::quit()
  • Automatically loads translation files from an i18n directory adjacent to the main QML file.
  • Automatically sets an incubation controller if the scene contains a QQuickWindow.
  • Automatically sets a QQmlFileSelector as the url interceptor, applying file selectors to all QML files and assets.

The engine behavior can be further tweaked by using the inherited methods from QQmlEngine.

Member Function Documentation

QQmlApplicationEngine::QQmlApplicationEngine(const QString &filePath, QObject *parent = 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(const QUrl &url, QObject *parent = 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(QObject *parent = nullptr)

Create a new QQmlApplicationEngine with the given parent. You will have to call load() later in order to load a QML file.

[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.

If an error occurs, error messages are printed with qWarning.

[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.

If an error occurs, the objectCreated signal is emitted with a null pointer as parameter and error messages are printed with 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.

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

If an error occurs, error messages are printed with 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.

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

Note: 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.

[override virtual] QQmlApplicationEngine::~QQmlApplicationEngine()

Destroys the QQmlApplicationEngine and all QML objects it loaded.

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.

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

© 2019 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.