Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
[SOLVED] How to access QQmlApplicationEngine?
morker last edited by
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");"
andresantos last edited by
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.
alexvplay last edited by
What's your class' superclass where you need the reference to the engine?
If you only need the instance of the current
QQuickItemyou can use a code snippet like the following:
QQmlContext *currentContext = QQmlEngine::contextForObject(this); QQmlEngine *engine = currentContext->engine();