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

后端 未结 2 419
逝去的感伤
逝去的感伤 2021-01-21 18:51

Alright. I have searched a lot but haven\'t got a good solution yet. I am new to Qt. I have a class which is a QQuickItem like so,

clas         


        
相关标签:
2条回答
  • 2021-01-21 19:06

    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.

    0 讨论(0)
  • 2021-01-21 19:15

    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");
    
    0 讨论(0)
提交回复
热议问题