PySide6.QtQml.QQmlEngine

class QQmlEngine

The QQmlEngine class provides an environment for instantiating QML components. More

Inheritance diagram of PySide6.QtQml.QQmlEngine

Inherited by: QQmlApplicationEngine

Synopsis

Properties

Methods

Slots

Signals

Static functions

Note

This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE

Detailed Description

A QQmlEngine is used to manage components and objects created from them and execute their bindings and functions. QQmlEngine also inherits from QJSEngine which allows seamless integration between your QML components and JavaScript code.

Each QML component is instantiated in a QQmlContext . In QML, contexts are arranged hierarchically and this hierarchy is managed by the QQmlEngine . By default, components are instantiated in the root context .

Note

Properties can be used directly when from __feature__ import true_property is used or via accessor functions otherwise.

property offlineStoragePathᅟ: str

This property holds the directory for storing offline user data.

Returns the directory where SQL and other offline storage is placed.

The SQL databases created with openDatabaseSync() are stored here.

The default is QML/OfflineStorage in the platform-standard user application data directory.

Note that the path may not currently exist on the filesystem, so callers wanting to create new files at this location should create it first - see QDir::mkpath().

See also

Qt Quick Local Storage QML Types

Access functions:
__init__([p=None])
Parameters:

pQObject

Create a new QQmlEngine with the given parent.

addImageProvider(id, arg__2)
Parameters:

Sets the provider to use for images requested via the image: url scheme, with host providerId. The QQmlEngine takes ownership of provider.

Image providers enable support for pixmap and threaded image requests. See the QQuickImageProvider documentation for details on implementing and using image providers.

All required image providers should be added to the engine before any QML sources files are loaded.

addImportPath(dir)
Parameters:

dir – str

Adds path as a directory where the engine searches for installed modules in a URL-based directory structure.

The path may be a local filesystem directory, a Qt Resource path (:/imports), a Qt Resource url (qrc:/imports) or a URL.

The path will be converted into canonical form before it is added to the import path list.

The newly added path will be first in the importPathList() .

See also setImportPathList() , QML Modules , and QML Import Path

addNamedBundle(arg__1, arg__2)
Parameters:
  • arg__1 – str

  • arg__2 – str

Return type:

bool

Note

This function is deprecated.

addPluginPath(dir)
Parameters:

dir – str

Adds path as a directory where the engine searches for native plugins for imported modules (referenced in the qmldir file).

By default, the list contains only ., i.e. the engine searches in the directory of the qmldir file itself.

The newly added path will be first in the pluginPathList() .

addUrlInterceptor(urlInterceptor)
Parameters:

urlInterceptorQQmlAbstractUrlInterceptor

Adds a urlInterceptor to be used when resolving URLs in QML. This also applies to URLs used for loading script files and QML types. The URL interceptors should not be modifed while the engine is loading files, or URL selection may be inconsistent. Multiple URL interceptors, when given, will be called in the order they were added for each URL.

QQmlEngine does not take ownership of the interceptor and won’t delete it.

baseUrl()
Return type:

QUrl

Return the base URL for this engine. The base URL is only used to resolve components when a relative URL is passed to the QQmlComponent constructor.

If a base URL has not been explicitly set, this method returns the application’s current working directory.

See also

setBaseUrl()

captureProperty(object, property)
Parameters:
clearComponentCache()

Clears the engine’s internal component cache.

This function causes the property metadata of most components previously loaded by the engine to be destroyed. It does so by dropping unreferenced components from the engine’s component cache. It does not drop components that are still referenced since that would almost certainly lead to crashes further down the line.

If no components are referenced, this function returns the engine to a state where it does not contain any loaded component data. This may be useful in order to reload a smaller subset of the previous component set, or to load a new version of a previously loaded component.

Once the component cache has been cleared, components must be loaded before any new objects can be created.

Note

Any existing objects created from QML components retain their types, even if you clear the component cache. This includes singleton objects. If you create more objects from the same QML code after clearing the cache, the new objects will be of different types than the old ones. Assigning such a new object to a property of its declared type belonging to an object created before clearing the cache won’t work.

As a general rule of thumb, make sure that no objects created from QML components are alive when you clear the component cache.

clearSingletons()

Clears all singletons the engine owns.

This function drops all singleton instances, deleting any QObjects owned by the engine among them. This is useful to make sure that no QML-created objects are left before calling clearComponentCache() .

QML properties holding QObject-based singleton instances become null if the engine owns the singleton or retain their value if the engine doesn’t own it. The singletons are not automatically re-created by accessing existing QML-created objects. Only when new components are instantiated, the singletons are re-created.

static contextForObject(arg__1)
Parameters:

arg__1QObject

Return type:

QQmlContext

Returns the QQmlContext for the object, or nullptr if no context has been set.

When the QQmlEngine instantiates a QObject, an internal context is assigned to it automatically. Such internal contexts are read-only. You cannot set context properties on them.

See also

setContextForObject() qmlContext() qmlEngine() setContextProperty()

exit(retCode)
Parameters:

retCode – int

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.

See also

quit()

imageProvider(id)
Parameters:

id – str

Return type:

QQmlImageProviderBase

Returns the image provider set for providerId if found; otherwise returns None.

importPathList()
Return type:

list of strings

Returns the list of directories where the engine searches for installed modules in a URL-based directory structure.

For example, if /opt/MyApp/lib/imports is in the path, then QML that imports com.mycompany.Feature will cause the QQmlEngine to look in /opt/MyApp/lib/imports/com/mycompany/Feature/ for the components provided by that module. A qmldir file is required for defining the type version mapping and possibly QML extensions plugins.

By default, this list contains the paths mentioned in QML Import Path .

importPlugin(filePath, uri, errors)
Parameters:
  • filePath – str

  • uri – str

  • errorsQList

Return type:

bool

Note

This function is deprecated.

Import the module from QML with an “import” statement instead.

Imports the plugin named filePath with the uri provided. Returns true if the plugin was successfully imported; otherwise returns false.

On failure and if non-null, the errors list will have any errors which occurred prepended to it.

The plugin has to be a Qt plugin which implements the QQmlEngineExtensionPlugin interface.

Note

Directly loading plugins like this can confuse the module import logic. In order to make the import logic load plugins from a specific place, you can use addPluginPath() . Each plugin should be part of a QML module that you can import using the “import” statement.

incubationController()
Return type:

QQmlIncubationController

Returns the currently set incubation controller, or 0 if no controller has been set.

interceptUrl(url, type)
Parameters:
Return type:

QUrl

Run the current URL interceptors on the given url of the given type and return the result.

markCurrentFunctionAsTranslationBinding()

If this method is called inside of a function that is part of a binding in QML, the binding will be treated as a translation binding.

class I18nAwareClass : public QObject {

  //...

   QString text() const
   {
        if (auto engine = qmlEngine(this))
            engine->markCurrentFunctionAsTranslationBinding();
        return tr("Hello, world!");
   }
};

Note

This function is mostly useful if you wish to provide your own alternative to the qsTr function. To ensure that properties exposed from C++ classes are updated on language changes, it is instead recommended to react to LanguageChange events. That is a more general mechanism which also works when the class is used in a non-QML context, and has slightly less overhead. However, using markCurrentFunctionAsTranslationBinding can be acceptable when the class is already closely tied to the QML engine. For more details, see Prepare for Dynamic Language Changes

See also

retranslate

networkAccessManager()
Return type:

QNetworkAccessManager

Returns a common QNetworkAccessManager which can be used by any QML type instantiated by this engine.

If a QQmlNetworkAccessManagerFactory has been set and a QNetworkAccessManager has not yet been created, the QQmlNetworkAccessManagerFactory will be used to create the QNetworkAccessManager; otherwise the returned QNetworkAccessManager will have no proxy or cache set.

networkAccessManagerFactory()
Return type:

QQmlNetworkAccessManagerFactory

Returns the current QQmlNetworkAccessManagerFactory .

offlineStorageDatabaseFilePath(databaseName)
Parameters:

databaseName – str

Return type:

str

Returns the file path where a Local Storage database with the identifier databaseName is (or would be) located.

See also

openDatabaseSync()

offlineStoragePath()
Return type:

str

Getter of property offlineStoragePathᅟ .

offlineStoragePathChanged()

This signal is emitted when offlineStoragePath changes.

Notification signal of property offlineStoragePathᅟ .

outputWarningsToStandardError()
Return type:

bool

Returns true if warning messages will be output to stderr in addition to being emitted by the warnings() signal, otherwise false.

The default value is true.

pluginPathList()
Return type:

list of strings

Returns the list of directories where the engine searches for native plugins for imported modules (referenced in the qmldir file).

By default, the list contains only ., i.e. the engine searches in the directory of the qmldir file itself.

quit()

This signal is emitted when the QML loaded by the engine would like to quit.

See also

exit()

removeImageProvider(id)
Parameters:

id – str

Removes the image provider for providerId.

removeUrlInterceptor(urlInterceptor)
Parameters:

urlInterceptorQQmlAbstractUrlInterceptor

Remove a urlInterceptor that was previously added using addUrlInterceptor . The URL interceptors should not be modifed while the engine is loading files, or URL selection may be inconsistent.

This does not delete the interceptor, but merely removes it from the engine. You can re-use it on the same or a different engine afterwards.

retranslate()

Refreshes all binding expressions that use strings marked for translation.

Call this function after you have installed a new translator with QCoreApplication::installTranslator, to ensure that your user-interface shows up-to-date translations.

rootContext()
Return type:

QQmlContext

Returns the engine’s root context.

The root context is automatically created by the QQmlEngine . Data that should be available to all QML component instances instantiated by the engine should be put in the root context.

Additional data that should only be available to a subset of component instances should be added to sub-contexts parented to the root context.

setBaseUrl(arg__1)
Parameters:

arg__1QUrl

Set the base URL for this engine to url.

See also

baseUrl()

static setContextForObject(arg__1, arg__2)
Parameters:

Sets the QQmlContext for the object to context. If the object already has a context, a warning is output, but the context is not changed.

When the QQmlEngine instantiates a QObject, the context is set automatically.

setImportPathList(paths)
Parameters:

paths – list of strings

Sets paths as the list of directories where the engine searches for installed modules in a URL-based directory structure.

By default, this list contains the paths mentioned in QML Import Path .

Warning

Calling setImportPathList does not preserve the default import paths.

setIncubationController(arg__1)
Parameters:

arg__1QQmlIncubationController

Sets the engine’s incubation controller. The engine can only have one active controller and it does not take ownership of it.

setNetworkAccessManagerFactory(arg__1)
Parameters:

arg__1QQmlNetworkAccessManagerFactory

Sets the factory to use for creating QNetworkAccessManager(s).

QNetworkAccessManager is used for all network access by QML. By implementing a factory it is possible to create custom QNetworkAccessManager with specialized caching, proxy and cookie support.

The factory must be set before executing the engine.

Note

QQmlEngine does not take ownership of the factory.

setOfflineStoragePath(dir)
Parameters:

dir – str

Setter of property offlineStoragePathᅟ .

setOutputWarningsToStandardError(arg__1)
Parameters:

arg__1 – bool

Set whether warning messages will be output to stderr to enabled.

If enabled is true, any warning messages generated by QML will be output to stderr and emitted by the warnings() signal. If enabled is false, only the warnings() signal will be emitted. This allows applications to handle warning output themselves.

The default value is true.

setPluginPathList(paths)
Parameters:

paths – list of strings

Sets the list of directories where the engine searches for native plugins for imported modules (referenced in the qmldir file) to paths.

By default, the list contains only ., i.e. the engine searches in the directory of the qmldir file itself.

setUrlInterceptor(urlInterceptor)
Parameters:

urlInterceptorQQmlAbstractUrlInterceptor

Note

This function is deprecated.

singletonInstance(qmlTypeId)
Parameters:

qmlTypeId – int

Return type:

QObject

Returns the instance of a singleton type that was registered under qmlTypeId. For QObject-derived singleton types, the QObject instance is returned, otherwise a QJSValue or None.

It is recommended to store the QML type id, e.g. as a static member in the singleton class. The lookup via qmlTypeId() is costly.

singletonInstance(uri, typeName)
Parameters:
  • uri – str

  • typeName – str

Return type:

QObject

trimComponentCache()

Trims the engine’s internal component cache.

This function causes the property metadata of any loaded components which are not currently in use to be destroyed.

A component is considered to be in use if there are any extant instances of the component itself, any instances of other components that use the component, or any objects instantiated by any of those components.

urlInterceptor()
Return type:

QQmlAbstractUrlInterceptor

Note

This function is deprecated.

This API is private for 5.1

Returns the last QQmlAbstractUrlInterceptor . It must not be modified outside the GUI thread.

urlInterceptors()
Return type:

.list of QQmlAbstractUrlInterceptor

Returns the list of currently active URL interceptors.

warnings(warnings)
Parameters:

warnings – .list of QQmlError

This signal is emitted when warnings messages are generated by QML.