[Solved] Access QML Singleton object from C++ code
-
Hi,
I would like to access a Singleton QML object declared via qmldir from C++ code. Is it possible? I have tried to search the children of root object from QQmlApplicationEngine but seem that singleton object is not its children .
Thanks for any advise.
-
Hi @benlau, Probably something like this:
//QML : main.qml property QtObject obj: MySingleTonObject // assuming MySingleTonObject is the singleton exposed through qmldir //C++ QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:///main.qml"))); QObject *obj = qvariant_cast<QObject *>(engine.rootObjects().first()->property("obj"));