QQmlEngine Class
The QQmlEngine class makes objects from QML documents. More…
| Header | #include <QQmlEngine> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Qml) |
| Module | Nt Qml C++ Classes |
| Inherits | QJSEngine |
| Inherited by | QQmlApplicationEngine |
List of all members, including inherited members
Public Functions
| QQmlEngine(QObject *parent = nullptr) | |
| ~QQmlEngine() | |
| QQmlContext * | rootContext() const |
| void | clearComponentCache() |
| void | trimComponentCache() |
| QStringList | importPathList() const |
| void | setImportPathList(const QStringList &paths) |
| void | addImportPath(const QString &dir) |
| QUrl | baseUrl() const |
| void | setBaseUrl(const QUrl &url) |
| QUrl | interceptUrl(const QUrl &url, int type) const |
| bool | outputWarningsToStandardError() const |
| void | setOutputWarningsToStandardError(bool enabled) |
| QString | offlineStoragePath() const |
| void | setOfflineStoragePath(const QString &dir) |
| T | singletonInstance(const QString &moduleName, const QString &typeName) |
Public Slots
| void | retranslate() |
Signals
Static Public Members
| QQmlContext * | contextForObject(const QObject *object) |
| void | setContextForObject(QObject *object, QQmlContext *context) |
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
QQmlEngine::QQmlEngine(QObject *parent = nullptr)
Constructs an engine with the given parent.
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.
QQmlContext * QQmlEngine::contextForObject(const QObject *object)
Returns the context that object was made in, or nullptr if QML did not make it.
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.
void QQmlEngine::quit()
This signal is emitted when QML code calls Qt.quit().
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.
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.
void QQmlEngine::warnings(const QList<QQmlError> &warnings)
This signal is emitted with warnings when QML code makes errors that are not fatal.