Important: Please read the Qt Code of Conduct -

Accessing to QML data model object from C++ directly

  • Hi guys.
    In my app I have C++ model and QML view.
    Model is registered in C++ with qmlRegisterType<DataModel>("cpp.DataModel", 1, 0, "DataModel");
    It is Ok to use it from QML, but how can I use this class's object from C++ again?

    qmlRegisterType<DataModel>("cpp.DataModel", 1, 0, "DataModel");
    DataModel *dataModelObject = <<<code to get object registered as QML type>>> //how to embody this?

  • With the code above you only register the Datatype!
    You create the instance(s) in the qml-Part.

    if you are using QQmlApplicationEngine you can search the rootObject for your instance you created in qml, or ( i think the simpler way ) create a instance and reqister it as ContextProperty.

    see Interacting with QML Objects from C++

Log in to reply