QQmlEngine#

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

Inheritance diagram of PySide6.QtQml.QQmlEngine

Inherited by: QQmlApplicationEngine

Synopsis#

Properties#

Functions#

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

class PySide6.QtQml.QQmlEngine([p=None])#
Parameters:

pPySide6.QtCore.QObject

Create a new QQmlEngine with the given parent.

Note

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

property PᅟySide6.QtQml.QQmlEngine.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:
PySide6.QtQml.QQmlEngine.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.

PySide6.QtQml.QQmlEngine.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

PySide6.QtQml.QQmlEngine.addNamedBundle(arg__1, arg__2)#
Parameters:
  • arg__1 – str

  • arg__2 – str

Return type:

bool

Note

This function is deprecated.

PySide6.QtQml.QQmlEngine.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() .

PySide6.QtQml.QQmlEngine.addUrlInterceptor(urlInterceptor)#
Parameters:

urlInterceptorPySide6.QtQml.QQmlAbstractUrlInterceptor

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.

PySide6.QtQml.QQmlEngine.baseUrl()#
Return type:

PySide6.QtCore.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()

PySide6.QtQml.QQmlEngine.captureProperty(object, property)#
Parameters:
PySide6.QtQml.QQmlEngine.clearComponentCache()#

Clears the engine’s internal component cache.

This function causes the property metadata of all components previously loaded by the engine to be destroyed. All previously loaded components and the property bindings for all extant objects created from those components will cease to function.

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.

PySide6.QtQml.QQmlEngine.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 PySide6.QtQml.QQmlEngine.contextForObject(arg__1)#
Parameters:

arg__1PySide6.QtCore.QObject

Return type:

PySide6.QtQml.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()

PySide6.QtQml.QQmlEngine.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()

PySide6.QtQml.QQmlEngine.imageProvider(id)#
Parameters:

id – str

Return type:

PySide6.QtQml.QQmlImageProviderBase

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

See also

QQuickImageProvider

PySide6.QtQml.QQmlEngine.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 .

PySide6.QtQml.QQmlEngine.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.

PySide6.QtQml.QQmlEngine.incubationController()#
Return type:

PySide6.QtQml.QQmlIncubationController

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

PySide6.QtQml.QQmlEngine.interceptUrl(url, type)#
Parameters:
Return type:

PySide6.QtCore.QUrl

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

PySide6.QtQml.QQmlEngine.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

PySide6.QtQml.QQmlEngine.networkAccessManager()#
Return type:

PySide6.QtNetwork.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.

PySide6.QtQml.QQmlEngine.networkAccessManagerFactory()#
Return type:

PySide6.QtQml.QQmlNetworkAccessManagerFactory

Returns the current QQmlNetworkAccessManagerFactory .

PySide6.QtQml.QQmlEngine.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()

PySide6.QtQml.QQmlEngine.offlineStoragePath()#
Return type:

str

Getter of property offlineStoragePath .

PySide6.QtQml.QQmlEngine.offlineStoragePathChanged()#

This signal is emitted when offlineStoragePath changes.

Notification signal of property offlineStoragePath .

PySide6.QtQml.QQmlEngine.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.

PySide6.QtQml.QQmlEngine.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.

PySide6.QtQml.QQmlEngine.quit()#

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

See also

exit()

PySide6.QtQml.QQmlEngine.removeImageProvider(id)#
Parameters:

id – str

Removes the image provider for providerId.

See also

addImageProvider() QQuickImageProvider

PySide6.QtQml.QQmlEngine.removeUrlInterceptor(urlInterceptor)#
Parameters:

urlInterceptorPySide6.QtQml.QQmlAbstractUrlInterceptor

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.

PySide6.QtQml.QQmlEngine.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.

PySide6.QtQml.QQmlEngine.rootContext()#
Return type:

PySide6.QtQml.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.

PySide6.QtQml.QQmlEngine.setBaseUrl(arg__1)#
Parameters:

arg__1PySide6.QtCore.QUrl

Set the base URL for this engine to url.

See also

baseUrl()

static PySide6.QtQml.QQmlEngine.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.

PySide6.QtQml.QQmlEngine.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.

PySide6.QtQml.QQmlEngine.setIncubationController(arg__1)#
Parameters:

arg__1PySide6.QtQml.QQmlIncubationController

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

PySide6.QtQml.QQmlEngine.setNetworkAccessManagerFactory(arg__1)#
Parameters:

arg__1PySide6.QtQml.QQmlNetworkAccessManagerFactory

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.

PySide6.QtQml.QQmlEngine.setOfflineStoragePath(dir)#
Parameters:

dir – str

Setter of property offlineStoragePath .

PySide6.QtQml.QQmlEngine.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.

PySide6.QtQml.QQmlEngine.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.

PySide6.QtQml.QQmlEngine.setUrlInterceptor(urlInterceptor)#
Parameters:

urlInterceptorPySide6.QtQml.QQmlAbstractUrlInterceptor

Note

This function is deprecated.

PySide6.QtQml.QQmlEngine.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.

PySide6.QtQml.QQmlEngine.urlInterceptor()#
Return type:

PySide6.QtQml.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.

PySide6.QtQml.QQmlEngine.urlInterceptors()#
Return type:

.list of QQmlAbstractUrlInterceptor

Returns the list of currently active URL interceptors.

PySide6.QtQml.QQmlEngine.warnings(warnings)#
Parameters:

warnings – .list of QQmlError

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