![]() |
Qt 6.x
The Qt SDK
|
QQmlApplicationEngine provides a convenient way to load an application from a single QML file. More...
#include <qqmlapplicationengine.h>
Public Slots | |
void | load (const QUrl &url) |
Loads the root QML file located at url. | |
void | load (const QString &filePath) |
Loads the root QML file located at filePath. | |
void | loadFromModule (QAnyStringView uri, QAnyStringView typeName) |
Loads the QML type typeName from the module specified by uri. | |
void | setInitialProperties (const QVariantMap &initialProperties) |
Sets the initialProperties with which the QML component gets initialized after it gets loaded. | |
void | setExtraFileSelectors (const QStringList &extraFileSelectors) |
Sets the extraFileSelectors to be passed to the internal QQmlFileSelector used for resolving URLs to local files. | |
void | loadData (const QByteArray &data, const QUrl &url=QUrl()) |
Loads the QML given in data. | |
![]() | |
void | retranslate () |
Refreshes all binding expressions that use strings marked for translation. | |
![]() | |
void | deleteLater () |
\threadsafe | |
Signals | |
void | objectCreated (QObject *object, const QUrl &url) |
This signal is emitted when an object finishes loading. | |
void | objectCreationFailed (const QUrl &url) |
![]() | |
void | offlineStoragePathChanged () |
This signal is emitted when \l offlineStoragePath changes. | |
void | quit () |
This signal is emitted when the QML loaded by the engine would like to quit. | |
void | exit (int retCode) |
This signal is emitted when the QML loaded by the engine would like to exit from the event loop with the specified return code retCode. | |
void | warnings (const QList< QQmlError > &warnings) |
This signal is emitted when warnings messages are generated by QML. | |
![]() | |
void | uiLanguageChanged () |
![]() | |
void | destroyed (QObject *=nullptr) |
This signal is emitted immediately before the object obj is destroyed, after any instances of QPointer have been notified, and cannot be blocked. | |
void | objectNameChanged (const QString &objectName, QPrivateSignal) |
This signal is emitted after the object's name has been changed. | |
Public Member Functions | |
QQmlApplicationEngine (QObject *parent=nullptr) | |
Create a new QQmlApplicationEngine with the given parent. | |
QQmlApplicationEngine (const QUrl &url, QObject *parent=nullptr) | |
Create a new QQmlApplicationEngine and loads the QML file at the given url. | |
QQmlApplicationEngine (QAnyStringView uri, QAnyStringView typeName, QObject *parent=nullptr) | |
Create a new QQmlApplicationEngine and loads the QML type specified by uri and typeName This is provided as a convenience, and is the same as using the empty constructor and calling loadFromModule afterwards. | |
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. | |
~QQmlApplicationEngine () override | |
Destroys the QQmlApplicationEngine and all QML objects it loaded. | |
QList< QObject * > | rootObjects () const |
Returns a list of all the root objects instantiated by the QQmlApplicationEngine. | |
![]() | |
QQmlEngine (QObject *p=nullptr) | |
Create a new QQmlEngine with the given parent. | |
~QQmlEngine () override | |
Destroys the QQmlEngine. | |
QQmlContext * | rootContext () const |
Returns the engine's root context. | |
void | clearComponentCache () |
Clears the engine's internal component cache. | |
void | trimComponentCache () |
Trims the engine's internal component cache. | |
void | clearSingletons () |
Clears all singletons the engine owns. | |
QStringList | importPathList () const |
Returns the list of directories where the engine searches for installed modules in a URL-based directory structure. | |
void | setImportPathList (const QStringList &paths) |
Sets paths as the list of directories where the engine searches for installed modules in a URL-based directory structure. | |
void | addImportPath (const QString &dir) |
Adds path as a directory where the engine searches for installed modules in a URL-based directory structure. | |
QStringList | pluginPathList () const |
Returns the list of directories where the engine searches for native plugins for imported modules (referenced in the qmldir file). | |
void | setPluginPathList (const QStringList &paths) |
Sets the list of directories where the engine searches for native plugins for imported modules (referenced in the qmldir file) to paths. | |
void | addPluginPath (const QString &dir) |
Adds path as a directory where the engine searches for native plugins for imported modules (referenced in the qmldir file). | |
void | addUrlInterceptor (QQmlAbstractUrlInterceptor *urlInterceptor) |
Adds a urlInterceptor to be used when resolving URLs in QML. | |
void | removeUrlInterceptor (QQmlAbstractUrlInterceptor *urlInterceptor) |
Remove a urlInterceptor that was previously added using \l addUrlInterceptor. | |
QList< QQmlAbstractUrlInterceptor * > | urlInterceptors () const |
Returns the list of currently active URL interceptors. | |
QUrl | interceptUrl (const QUrl &url, QQmlAbstractUrlInterceptor::DataType type) const |
Run the current URL interceptors on the given url of the given type and return the result. | |
void | addImageProvider (const QString &id, QQmlImageProviderBase *) |
Sets the provider to use for images requested via the image: url scheme, with host providerId. | |
QQmlImageProviderBase * | imageProvider (const QString &id) const |
Returns the image provider set for providerId if found; otherwise returns \nullptr. | |
void | removeImageProvider (const QString &id) |
Removes the image provider for providerId. | |
void | setIncubationController (QQmlIncubationController *) |
Sets the engine's incubation controller. | |
QQmlIncubationController * | incubationController () const |
Returns the currently set incubation controller, or 0 if no controller has been set. | |
void | setOfflineStoragePath (const QString &dir) |
QString | offlineStoragePath () const |
QString | offlineStorageDatabaseFilePath (const QString &databaseName) const |
Returns the file path where a \l{QtQuick.LocalStorage}{Local Storage} database with the identifier databaseName is (or would be) located. | |
QUrl | baseUrl () const |
Return the base URL for this engine. | |
void | setBaseUrl (const QUrl &) |
Set the base URL for this engine to url. | |
bool | outputWarningsToStandardError () const |
Returns true if warning messages will be output to stderr in addition to being emitted by the warnings() signal, otherwise false. | |
void | setOutputWarningsToStandardError (bool) |
Set whether warning messages will be output to stderr to enabled. | |
void | markCurrentFunctionAsTranslationBinding () |
template<typename T > | |
T | singletonInstance (int qmlTypeId) |
\qmlproperty string Qt::uiLanguage | |
template<typename T > | |
T | singletonInstance (QAnyStringView moduleName, QAnyStringView typeName) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Returns the instance of a singleton type named typeName from the module specified by uri. | |
void | captureProperty (QObject *object, const QMetaProperty &property) const |
template<> | |
QJSValue | singletonInstance (int qmlTypeId) |
template<> | |
QJSValue | singletonInstance (QAnyStringView uri, QAnyStringView typeName) |
template<> | |
Q_QML_EXPORT QJSValue | singletonInstance (int qmlTypeId) |
template<> | |
Q_QML_EXPORT QJSValue | singletonInstance (QAnyStringView uri, QAnyStringView typeName) |
![]() | |
QJSEngine () | |
Constructs a QJSEngine object. | |
QJSEngine (QObject *parent) | |
Constructs a QJSEngine object with the given parent. | |
~QJSEngine () override | |
Destroys this QJSEngine. | |
QJSValue | globalObject () const |
Returns this engine's Global Object. | |
QJSValue | evaluate (const QString &program, const QString &fileName=QString(), int lineNumber=1, QStringList *exceptionStackTrace=nullptr) |
Evaluates program, using lineNumber as the base line number, and returns the result of the evaluation. | |
QJSValue | importModule (const QString &fileName) |
Imports the module located at fileName and returns a module namespace object that contains all exported variables, constants and functions as properties. | |
bool | registerModule (const QString &moduleName, const QJSValue &value) |
Registers a QJSValue to serve as a module. | |
QJSValue | newObject () |
Creates a JavaScript object of class Object. | |
QJSValue | newSymbol (const QString &name) |
QJSValue | newArray (uint length=0) |
Creates a JavaScript object of class Array with the given length. | |
QJSValue | newQObject (QObject *object) |
Creates a JavaScript object that wraps the given QObject object, using JavaScriptOwnership. | |
QJSValue | newQMetaObject (const QMetaObject *metaObject) |
template<typename T > | |
QJSValue | newQMetaObject () |
QJSValue | newErrorObject (QJSValue::ErrorType errorType, const QString &message=QString()) |
template<typename T > | |
QJSValue | toScriptValue (const T &value) |
Creates a QJSValue with the given value. | |
template<typename T > | |
QJSManagedValue | toManagedValue (const T &value) |
Creates a QJSManagedValue with the given value. | |
template<typename T > | |
QJSPrimitiveValue | toPrimitiveValue (const T &value) |
Creates a QJSPrimitiveValue with the given value. | |
template<typename T > | |
T | fromScriptValue (const QJSValue &value) |
Returns the given value converted to the template type {T}. | |
template<typename T > | |
T | fromManagedValue (const QJSManagedValue &value) |
Returns the given value converted to the template type {T}. | |
template<typename T > | |
T | fromPrimitiveValue (const QJSPrimitiveValue &value) |
Returns the given value converted to the template type {T}. | |
template<typename T > | |
T | fromVariant (const QVariant &value) |
Returns the given value converted to the template type {T}. | |
template<typename From , typename To > | |
To | coerceValue (const From &from) |
Returns the given from converted to the template type {To}. | |
void | collectGarbage () |
Runs the garbage collector. | |
void | installExtensions (Extensions extensions, const QJSValue &object=QJSValue()) |
void | setInterrupted (bool interrupted) |
bool | isInterrupted () const |
QV4::ExecutionEngine * | handle () const |
void | throwError (const QString &message) |
Throws a run-time error (exception) with the given message. | |
void | throwError (QJSValue::ErrorType errorType, const QString &message=QString()) |
void | throwError (const QJSValue &error) |
bool | hasError () const |
Returns true if the last JavaScript execution resulted in an exception or if throwError() was called. | |
QJSValue | catchError () |
If an exception is currently pending, catches it and returns it as a QJSValue. | |
QString | uiLanguage () const |
void | setUiLanguage (const QString &language) |
![]() | |
Q_INVOKABLE | QObject (QObject *parent=nullptr) |
Constructs an object with parent object parent. | |
virtual | ~QObject () |
Destroys the object, deleting all its child objects. | |
virtual bool | event (QEvent *event) |
This virtual function receives events to an object and should return true if the event e was recognized and processed. | |
virtual bool | eventFilter (QObject *watched, QEvent *event) |
Filters events if this object has been installed as an event filter for the watched object. | |
QString | objectName () const |
Q_WEAK_OVERLOAD void | setObjectName (const QString &name) |
Sets the object's name to name. | |
void | setObjectName (QAnyStringView name) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
QBindable< QString > | bindableObjectName () |
bool | isWidgetType () const |
Returns true if the object is a widget; otherwise returns false . | |
bool | isWindowType () const |
Returns true if the object is a window; otherwise returns false . | |
bool | isQuickItemType () const |
Returns true if the object is a QQuickItem; otherwise returns false . | |
bool | signalsBlocked () const noexcept |
Returns true if signals are blocked; otherwise returns false . | |
bool | blockSignals (bool b) noexcept |
If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it). | |
QThread * | thread () const |
Returns the thread in which the object lives. | |
void | moveToThread (QThread *thread) |
Changes the thread affinity for this object and its children. | |
int | startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer) |
This is an overloaded function that will start a timer of type timerType and a timeout of interval milliseconds. | |
int | startTimer (std::chrono::milliseconds time, Qt::TimerType timerType=Qt::CoarseTimer) |
void | killTimer (int id) |
Kills the timer with timer identifier, id. | |
template<typename T > | |
T | findChild (const QString &aName=QString(), Qt::FindChildOptions options=Qt::FindChildrenRecursively) const |
Returns the child of this object that can be cast into type T and that is called name, or \nullptr if there is no such object. | |
template<typename T > | |
QList< T > | findChildren (const QString &aName, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const |
Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects. | |
template<typename T > | |
QList< T > | findChildren (Qt::FindChildOptions options=Qt::FindChildrenRecursively) const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
const QObjectList & | children () const |
Returns a list of child objects. | |
void | setParent (QObject *parent) |
Makes the object a child of parent. | |
void | installEventFilter (QObject *filterObj) |
Installs an event filter filterObj on this object. | |
void | removeEventFilter (QObject *obj) |
Removes an event filter object obj from this object. | |
QMetaObject::Connection | connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const |
bool | disconnect (const char *signal=nullptr, const QObject *receiver=nullptr, const char *member=nullptr) const |
bool | disconnect (const QObject *receiver, const char *member=nullptr) const |
void | dumpObjectTree () const |
Dumps a tree of children to the debug output. | |
void | dumpObjectInfo () const |
Dumps information about signal connections, etc. | |
bool | setProperty (const char *name, const QVariant &value) |
Sets the value of the object's name property to value. | |
bool | setProperty (const char *name, QVariant &&value) |
QVariant | property (const char *name) const |
Returns the value of the object's name property. | |
QList< QByteArray > | dynamicPropertyNames () const |
QBindingStorage * | bindingStorage () |
const QBindingStorage * | bindingStorage () const |
QObject * | parent () const |
Returns a pointer to the parent object. | |
bool | inherits (const char *classname) const |
Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false . | |
Additional Inherited Members | |
![]() | |
enum | ObjectOwnership { CppOwnership , JavaScriptOwnership } |
ObjectOwnership controls whether or not the JavaScript memory manager automatically destroys the QObject when the corresponding JavaScript object is garbage collected by the engine. More... | |
enum | Extension { TranslationExtension = 0x1 , ConsoleExtension = 0x2 , GarbageCollectionExtension = 0x4 , AllExtensions = 0xffffffff } |
This enum is used to specify extensions to be installed via \l installExtensions(). More... | |
![]() | |
static QQmlContext * | contextForObject (const QObject *) |
Returns the QQmlContext for the object, or nullptr if no context has been set. | |
static void | setContextForObject (QObject *, QQmlContext *) |
Sets the QQmlContext for the object to context. | |
![]() | |
static void | setObjectOwnership (QObject *, ObjectOwnership) |
Sets the ownership of object. | |
static ObjectOwnership | objectOwnership (QObject *) |
Returns the ownership of object. | |
![]() | |
static QMetaObject::Connection | connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection) |
\threadsafe | |
static QMetaObject::Connection | connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection) |
template<typename Func1 , typename Func2 > | |
static QMetaObject::Connection | connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::ContextTypeForFunctor< Func2 >::ContextType *context, Func2 &&slot, Qt::ConnectionType type=Qt::AutoConnection) |
template<typename Func1 , typename Func2 > | |
static QMetaObject::Connection | connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, Func2 &&slot) |
static bool | disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member) |
\threadsafe | |
static bool | disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member) |
static bool | disconnect (const QMetaObject::Connection &) |
Disconnect a connection. | |
template<typename Func1 , typename Func2 > | |
static bool | disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer< Func2 >::Object *receiver, Func2 slot) |
template<typename Func1 > | |
static bool | disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const QObject *receiver, void **zero) |
![]() | |
QQmlEngine (QQmlEnginePrivate &dd, QObject *p) | |
bool | event (QEvent *) override |
\reimp | |
![]() | |
QJSEngine (QJSEnginePrivate &dd, QObject *parent=nullptr) | |
![]() | |
QObject * | sender () const |
Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns \nullptr. | |
int | senderSignalIndex () const |
int | receivers (const char *signal) const |
Returns the number of receivers connected to the signal. | |
bool | isSignalConnected (const QMetaMethod &signal) const |
virtual void | timerEvent (QTimerEvent *event) |
This event handler can be reimplemented in a subclass to receive timer events for the object. | |
virtual void | childEvent (QChildEvent *event) |
This event handler can be reimplemented in a subclass to receive child events. | |
virtual void | customEvent (QEvent *event) |
This event handler can be reimplemented in a subclass to receive custom events. | |
virtual void | connectNotify (const QMetaMethod &signal) |
virtual void | disconnectNotify (const QMetaMethod &signal) |
QObject (QObjectPrivate &dd, QObject *parent=nullptr) | |
![]() | |
QScopedPointer< QObjectData > | d_ptr |
![]() | |
QString | offlineStoragePath |
the directory for storing offline user data | |
![]() | |
QString | uiLanguage |
the language to be used for translating user interface strings | |
![]() | |
QString | objectName |
the name of this object | |
![]() | |
void | qmlRegisterModuleImport (const char *uri, int moduleMajor, const char *import, int importMajor, int importMinor) |
Registers a qmldir-import for module uri of major version moduleMajor. | |
void | qmlUnregisterModuleImport (const char *uri, int moduleMajor, const char *import, int importMajor, int importMinor) |
Removes a module import previously registered with qmlRegisterModuleImport() | |
QQmlEngine * | qmlEngine (const QObject *object) |
Returns the QQmlEngine associated with object, if any. | |
QQmlContext * | qmlContext (const QObject *object) |
Returns the QQmlContext associated with object, if any. | |
![]() | |
QJSEngine * | qjsEngine (const QObject *object) |
![]() | |
template< class T > T | qobject_cast (const QObject *object) |
Returns the given object cast to type T if the object is of type T (or of a subclass); otherwise returns \nullptr. | |
template< typename T > T | qFindChildqFindChildren (const QObject *obj, const QString &name)() |
template< typename T > QList< T > | qFindChildrenqFindChildren (const QObject *obj, const QString &name)() |
QObjectList | |
\macro Q_CLASSINFO(Name, Value) | |
QQmlApplicationEngine provides a convenient way to load an application from a single QML file.
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:
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 \l [QML] {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:
\list
QJSEngine::uiLanguage
/ Qt.uiLanguage
property is changed. QQmlFileSelector
as the url interceptor, applying file selectors to all QML files and assets. \endlistThe engine behavior can be further tweaked by using the inherited methods from QQmlEngine.
Definition at line 16 of file qqmlapplicationengine.h.
Create a new QQmlApplicationEngine with the given parent.
You will have to call load() later in order to load a QML file.
Definition at line 258 of file qqmlapplicationengine.cpp.
References QJSEnginePrivate::addToDebugServer().
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.
Definition at line 268 of file qqmlapplicationengine.cpp.
|
explicit |
Create a new QQmlApplicationEngine and loads the QML type specified by uri and typeName This is provided as a convenience, and is the same as using the empty constructor and calling loadFromModule afterwards.
Definition at line 282 of file qqmlapplicationengine.cpp.
References loadFromModule(), and typeName.
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.
Definition at line 296 of file qqmlapplicationengine.cpp.
|
override |
Destroys the QQmlApplicationEngine and all QML objects it loaded.
Definition at line 304 of file qqmlapplicationengine.cpp.
References d, and QJSEnginePrivate::removeFromDebugServer().
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.
Definition at line 335 of file qqmlapplicationengine.cpp.
References QUrl::AssumeLocalFile, d, and QUrl::fromUserInput().
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 \l {QQmlApplicationEngine::objectCreated()}{objectCreated} signal to determine when the object tree is ready.
If an error occurs, the \l {QQmlApplicationEngine::objectCreated()}{objectCreated} signal is emitted with a null pointer as parameter and error messages are printed with qWarning.
Definition at line 321 of file qqmlapplicationengine.cpp.
Referenced by QQmlApplicationEngine().
|
slot |
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.
Definition at line 422 of file qqmlapplicationengine.cpp.
|
slot |
Loads the QML type typeName from the module specified by uri.
If the type originates from a QML file located at a remote url, the type will be loaded asynchronously. Listen to the \l {QQmlApplicationEngine::objectCreated()}{objectCreated} signal to determine when the object tree is ready.
If an error occurs, the \l {QQmlApplicationEngine::objectCreated()}{objectCreated} signal is emitted with a null pointer as parameter and error messages are printed with qWarning.
Definition at line 360 of file qqmlapplicationengine.cpp.
Referenced by QQmlApplicationEngine().
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.
|
signal |
This signal is emitted when loading finishes because an error occurred.
The url to the component that failed to load is provided as an argument.
See also \l {QQmlApplicationEngine::objectCreated}, which will be emitted in addition to this signal (even though creation failed).
Returns a list of all the root objects instantiated by the QQmlApplicationEngine.
This will only contain objects loaded via load() or a convenience constructor.
{const}. Definition at line 436 of file qqmlapplicationengine.cpp.
References d.
|
slot |
Sets the extraFileSelectors to be passed to the internal QQmlFileSelector used for resolving URLs to local files.
The extraFileSelectors are applied when the first QML file is loaded. Setting them afterwards has no effect.
Definition at line 402 of file qqmlapplicationengine.cpp.
|
slot |
Sets the initialProperties with which the QML component gets initialized after it gets loaded.
Definition at line 387 of file qqmlapplicationengine.cpp.
References d.