[Solved] Access QML Singleton object from C++ code
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"));
hi @p3c0 ,
Thx! Seem that it is the only solution for this problem.
@benlau You're Welcome :)
Please mark it as solved if done.