6.0.0-rc4Source

QQmlEngine Class

The QQmlEngine class makes objects from QML documents. More…

Header#include <QQmlEngine>
CMaketarget_link_libraries(app PRIVATE Nt::Qml)
ModuleNt Qml C++ Classes
InheritsQJSEngine
Inherited byQQmlApplicationEngine

List of all members, including inherited members

Public Functions

QQmlEngine(QObject *parent = nullptr)
~QQmlEngine()
QQmlContext *rootContext() const
voidclearComponentCache()
voidtrimComponentCache()
QStringListimportPathList() const
voidsetImportPathList(const QStringList &paths)
voidaddImportPath(const QString &dir)
QUrlbaseUrl() const
voidsetBaseUrl(const QUrl &url)
QUrlinterceptUrl(const QUrl &url, int type) const
booloutputWarningsToStandardError() const
voidsetOutputWarningsToStandardError(bool enabled)
QStringofflineStoragePath() const
voidsetOfflineStoragePath(const QString &dir)
TsingletonInstance(const QString &moduleName, const QString &typeName)

Public Slots

voidretranslate()

Signals

voidquit()
voidexit(int retCode)
voidwarnings(const QList<QQmlError> &warnings)

Static Public Members

Detailed Description

An engine holds the types that QML documents can use, the documents it has read, and the root context in which components are made. Make objects with QQmlComponent, or use QQmlApplicationEngine, which loads a document and keeps its objects:

QQmlEngine engine;
engine.rootContext()->setContextProperty("greeting", QStringLiteral("hello"));
QObject::connect(&engine, &QQmlEngine::quit, &app, &QCoreApplication::quit);

QQmlComponent component(&engine);
component.setData(document, QUrl(QStringLiteral("file:///home.qml")));
QObject *home = component.create();
if (!home) {
    qWarning().noquote() << component.errorString();
    return 1;
}

The engine is a QJSEngine whose global object has console, print(), qsTr() and the Qt object: Qt.binding(), Qt.callLater(), Qt.quit(), Qt.point(), Qt.size(), Qt.rect(), Qt.formatDate(), Qt.formatTime(), Qt.formatDateTime(), Qt.createComponent(), Qt.createQmlObject(), Qt.resolvedUrl(), Qt.md5(), Qt.btoa(), Qt.atob(), Qt.platform.os and Qt.application.

Errors in bindings and handlers do not stop the application: they are reported with warnings(), and printed unless setOutputWarningsToStandardError() turns that off.

See also QQmlComponent, QQmlContext, QQmlApplicationEngine and Using Nt QML.

Differences from Qt

Documents are interpreted, not compiled. There are no network URLs, image providers, URL interceptors, plugins or offline storage. QML types are registered with qmlRegisterType() and the other functions of <qqml.h>; the QML_ELEMENT macros compile but do nothing. retranslate() does nothing, as Nt has no translations.

Member Function Documentation

[explicit] QQmlEngine::QQmlEngine(QObject *parent = nullptr)

Constructs an engine with the given parent.

[override] QQmlEngine::~QQmlEngine()

Destroys the engine. The objects made from QML that it still has must be deleted first.

void QQmlEngine::addImportPath(const QString &dir)

Adds dir to the directories where modules are looked for, before the others.

QUrl QQmlEngine::baseUrl() const

Returns the URL that relative URLs are resolved against, the current directory if it was not set.

void QQmlEngine::clearComponentCache()

Forgets the documents that were read, so that they are read again when they are next used.

[static] QQmlContext * QQmlEngine::contextForObject(const QObject *object)

Returns the context that object was made in, or nullptr if QML did not make it.

[signal] void QQmlEngine::exit(int retCode)

This signal is emitted when QML code calls Qt.exit() with retCode.

QStringList QQmlEngine::importPathList() const

Returns the directories where modules are looked for.

QUrl QQmlEngine::interceptUrl(const QUrl &url, int type) const

Returns url; Nt has no URL interceptors. type is not used.

QString QQmlEngine::offlineStoragePath() const

Returns the directory for offline storage. Nt has none, so it is empty unless set.

bool QQmlEngine::outputWarningsToStandardError() const

Returns true if warnings are printed as well as emitted with warnings(); otherwise returns false.

[signal] void QQmlEngine::quit()

This signal is emitted when QML code calls Qt.quit().

[slot] void QQmlEngine::retranslate()

Does nothing: Nt has no translations.

QQmlContext * QQmlEngine::rootContext() const

Returns the root context, the parent of the contexts that components are made in.

void QQmlEngine::setBaseUrl(const QUrl &url)

Sets the URL that relative URLs are resolved against to url.

[static] void QQmlEngine::setContextForObject(QObject *object, QQmlContext *context)

Sets the context of object to context.

void QQmlEngine::setImportPathList(const QStringList &paths)

Sets the directories where modules are looked for to paths.

void QQmlEngine::setOfflineStoragePath(const QString &dir)

Sets the directory for offline storage to dir. Nt keeps it but stores nothing.

void QQmlEngine::setOutputWarningsToStandardError(bool enabled)

Prints warnings if enabled is true, the default.

template <typename T>T QQmlEngine::singletonInstance(const QString &moduleName, const QString &typeName)

Returns the instance of the singleton typeName of moduleName, cast to T, or nullptr.

void QQmlEngine::trimComponentCache()

Forgets the documents that were read; the same as clearComponentCache() in Nt.

[signal] void QQmlEngine::warnings(const QList<QQmlError> &warnings)

This signal is emitted with warnings when QML code makes errors that are not fatal.