PySide6.QtQml.QQmlEngine¶
- class QQmlEngine¶
The
QQmlEngine
class provides an environment for instantiating QML components. More…Inherited by:
QQmlApplicationEngine
Synopsis¶
Properties¶
offlineStoragePathᅟ
- The directory for storing offline user data
Methods¶
def
__init__()
def
addImportPath()
def
addNamedBundle()
def
addPluginPath()
def
baseUrl()
def
imageProvider()
def
importPathList()
def
importPlugin()
def
interceptUrl()
def
pluginPathList()
def
rootContext()
def
setBaseUrl()
def
urlInterceptor()
Slots¶
def
retranslate()
Signals¶
def
exit()
def
quit()
def
warnings()
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 managecomponents
and objects created from them and execute their bindings and functions.QQmlEngine
also inherits fromQJSEngine
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 theQQmlEngine
. By default, components are instantiated in theroot 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:
Create a new
QQmlEngine
with the givenparent
.- addImageProvider(id, arg__2)¶
- Parameters:
id – str
arg__2 –
QQmlImageProviderBase
Sets the
provider
to use for images requested via the image: url scheme, with hostproviderId
. TheQQmlEngine
takes ownership ofprovider
.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 theimportPathList()
.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 theqmldir
file).By default, the list contains only
.
, i.e. the engine searches in the directory of theqmldir
file itself.The newly added
path
will be first in thepluginPathList()
.See also
- addUrlInterceptor(urlInterceptor)¶
- Parameters:
urlInterceptor –
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.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
- captureProperty(object, property)¶
- Parameters:
object –
QObject
property –
QMetaProperty
- 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.
See also
- 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.
See also
Returns the
QQmlContext
for theobject
, 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
- imageProvider(id)¶
- Parameters:
id – str
- Return type:
Returns the image provider set for
providerId
if found; otherwise returnsNone
.See also
- 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 importscom.mycompany.Feature
will cause theQQmlEngine
to look in/opt/MyApp/lib/imports/com/mycompany/Feature/
for the components provided by that module. Aqmldir
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 .
See also
- importPlugin(filePath, uri, errors)¶
- Parameters:
filePath – str
uri – str
errors –
QList
- Return type:
bool
Note
This function is deprecated.
Import the module from QML with an “import” statement instead.
Imports the plugin named
filePath
with theuri
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:
Returns the currently set incubation controller, or 0 if no controller has been set.
See also
Run the current URL interceptors on the given
url
of the giventype
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, usingmarkCurrentFunctionAsTranslationBinding
can be acceptable when the class is already closely tied to the QML engine. For more details, see Prepare for Dynamic Language ChangesSee also
- networkAccessManager()¶
- Return type:
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, theQQmlNetworkAccessManagerFactory
will be used to create the QNetworkAccessManager; otherwise the returned QNetworkAccessManager will have no proxy or cache set.See also
- networkAccessManagerFactory()¶
- Return type:
Returns the current
QQmlNetworkAccessManagerFactory
.See also
- 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
See also
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.
See also
- 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 theqmldir
file itself.See also
- quit()¶
This signal is emitted when the QML loaded by the engine would like to quit.
See also
- removeImageProvider(id)¶
- Parameters:
id – str
Removes the image provider for
providerId
.See also
- removeUrlInterceptor(urlInterceptor)¶
- Parameters:
urlInterceptor –
QQmlAbstractUrlInterceptor
Remove a
urlInterceptor
that was previously added usingaddUrlInterceptor
. 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:
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.
Set the base URL for this engine to
url
.See also
- static setContextForObject(arg__1, arg__2)¶
- Parameters:
arg__1 –
QObject
arg__2 –
QQmlContext
Sets the
QQmlContext
for theobject
tocontext
. If theobject
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.See also
- 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.
See also
- setIncubationController(arg__1)¶
- Parameters:
arg__1 –
QQmlIncubationController
Sets the engine’s incubation
controller
. The engine can only have one active controller and it does not take ownership of it.See also
- setNetworkAccessManagerFactory(arg__1)¶
- Parameters:
arg__1 –
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.
- setOfflineStoragePath(dir)¶
- Parameters:
dir – str
See also
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 thewarnings()
signal. Ifenabled
is false, only thewarnings()
signal will be emitted. This allows applications to handle warning output themselves.The default value is true.
See also
- 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) topaths
.By default, the list contains only
.
, i.e. the engine searches in the directory of theqmldir
file itself.See also
- setUrlInterceptor(urlInterceptor)¶
- Parameters:
urlInterceptor –
QQmlAbstractUrlInterceptor
Note
This function is deprecated.
Returns the instance of a singleton type that was registered under qmlTypeId. For
QObject
-derived singleton types, theQObject
instance is returned, otherwise aQJSValue
orNone
.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:
- 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.
See also
- urlInterceptor()¶
- Return type:
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.