Qt 6.11 is out! See what's new in the release
blog
[SOLVED] How to access QQmlApplicationEngine?
QML and Qt Quick
3
Posts
3
Posters
3.0k
Views
2
Watching
-
Hi,
I set in my main method my QQmlApplicationEngine and load my main.qml. I need to access the engine in another class.
What is the best approach to do that? Global variable? Something similar to "QSqlDatabase::database("myDatabase");"
Thanks
-
Not sure if this is the best method, but I would create a singleton with a QQmlApplicationEngine pointer variable and in your main pass the QQmlApplicationEngine instance to the singleton. This way you can access it everywhere.
-
What's your class' superclass where you need the reference to the engine?
If you only need the instance of the current
QQmlEnginewithin aQQuickItemyou can use a code snippet like the following:QQmlContext *currentContext = QQmlEngine::contextForObject(this); QQmlEngine *engine = currentContext->engine();