How to get a valid instance of a QQuickItem on C++ side

百般思念 提交于 2019-12-02 01:09:48

Generally it is a good idea to avoid accessing QML instantiated objects from outside as most of the access methods generated a dependency from C++ toward QML, restricting the way the QML tree is done.

E.g. requiring certain objects to exist at certain point in times, having specific objectName values, etc.

It is better to either "register" the object from QML side by calling a method on an exposed C++ object/API or to make the QML instantiate object register itself from within its own C++ code.

The latter is obviously inherently automatic, i.e. each instance of such a class would do that, while the former puts it at the discretion of the QML code which of the created instances it wants to make known.

Game_Of_Threads

Doing the following from a suggestion in discussion here solves the issue & gets a valid object to the QuickItem qml file

QQuickItem *myItem = engine.rootObjects()[0]->findChild<QQuickItem *>("myQuickItem");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!