PySide6.QtQml.QQmlApplicationEngine¶
- class QQmlApplicationEngine¶
- QQmlApplicationEngineprovides a convenient way to load an application from a single QML file. More…- Synopsis¶- Methods¶- def - __init__()
- def - rootObjects()
 - Slots¶- def - load()
- def - loadData()
- def - loadFromModule()
 - Signals¶- 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¶- This class combines a - QQmlEngineand- QQmlComponentto 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: - #include <QGuiApplication> #include <QQmlApplicationEngine> int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine("main.qml"); return app.exec(); } - Unlike QQuickView, - QQmlApplicationEnginedoes not automatically create a root window. If you are using visual items from Qt Quick, you will need to place them inside of a 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:- Connecting Qt. - quit()to QCoreApplication::quit()
- Automatically loads translation files from an i18n directory adjacent to the main QML file. - Translation files must have “qml_” prefix e.g. qml_ja_JP.qm. 
 
- Translations are reloaded when the - QJSEngine::uiLanguage/- Qt.uiLanguageproperty is changed.
- Automatically sets an incubation controller if the scene contains a QQuickWindow. 
- Automatically sets a - QQmlFileSelectoras the url interceptor, applying file selectors to all QML files and assets.
 - The engine behavior can be further tweaked by using the inherited methods from - QQmlEngine.- Create a new - QQmlApplicationEnginewith the given- parent. You will have to call- load()later in order to load a QML file.- __init__(filePath[, parent=None])
- Parameters:
- filePath – str 
- parent – - QObject
 
 
 - Create a new - QQmlApplicationEngineand loads the QML file at the given- filePath, which must be a local file or qrc 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. - Create a new - QQmlApplicationEngineand 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.- __init__(uri, typeName[, parent=None])
- Parameters:
- uri – str 
- typeName – str 
- parent – - QObject
 
 
 - Create a new - QQmlApplicationEngineand loads the QML type specified by- uriand- typeNameThis is provided as a convenience, and is the same as using the empty constructor and calling- loadFromModuleafterwards.- load(filePath)¶
- Parameters:
- filePath – str 
 
 - Loads the root QML file located at - filePath.- filePathmust be a path to a local file or a path to a file in the resource file system. If- filePathis 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. - load(url)
- Parameters:
- url – - QUrl
 
 - 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- objectCreatedsignal to determine when the object tree is ready.- If an error occurs, the - objectCreatedsignal is emitted with a null pointer as parameter and error messages are printed with qWarning.- loadData(data[, url=QUrl()])¶
- Parameters:
- data – - QByteArray
- url – - QUrl
 
 
 - Loads the QML given in - data. The object tree defined by- datais instantiated immediately.- If a - urlis 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. - loadFromModule(uri, typeName)¶
- Parameters:
- uri – str 
- typeName – str 
 
 
 - Loads the QML type - typeNamefrom 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- objectCreatedsignal to determine when the object tree is ready.- If an error occurs, the - objectCreatedsignal is emitted with a null pointer as parameter and error messages are printed with qWarning.- QQmlApplicationEngine engine; engine.loadFromModule("QtQuick", "Rectangle"); - Note - The module identified by - uriis searched in the import path , in the same way as if you were doing- import uriinside a QML file. If the module cannot be located there, this function will fail.- See also - This signal is emitted when an object finishes loading. If loading was successful, - objectcontains a pointer to the loaded object, otherwise the pointer is NULL.- The - urlto the component the- objectcame from is also provided.- Note - If the path to the component was provided as a QString containing a relative path, the - urlwill contain a fully resolved path to the file.- This signal is emitted when loading finishes because an error occurred. - The - urlto the component that failed to load is provided as an argument.- QGuiApplication app(argc, argv); QQmlApplicationEngine engine; // exit on error QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed, &app, []() { QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.load(QUrl()); return app.exec(); - Note - If the path to the component was provided as a QString containing a relative path, the - urlwill contain a fully resolved path to the file.- See also - 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.- Note - In Qt versions prior to 5.9, this function is marked as non- - const.- setExtraFileSelectors(extraFileSelectors)¶
- Parameters:
- extraFileSelectors – list of strings 
 
 - Sets the - extraFileSelectorsto be passed to the internal- QQmlFileSelectorused for resolving URLs to local files. The- extraFileSelectorsare applied when the first QML file is loaded. Setting them afterwards has no effect.- See also - QQmlFileSelector- setExtraSelectors- setInitialProperties(initialProperties)¶
- Parameters:
- initialProperties – Dictionary with keys of type .QString and values of type QVariant. 
 
 - Sets the - initialPropertieswith which the QML component gets initialized after it gets loaded.- QQmlApplicationEngine engine; EventDatabase eventDatabase; EventMonitor eventMonitor; engine.setInitialProperties({ { "eventDatabase", QVariant::fromValue(&eventDatabase) }, { "eventMonitor", QVariant::fromValue(&eventMonitor) } }); - See also