Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QML & Object.defineProperty



  • Hi;

    How to read property of QML object that defined by Object.defineProperty from C++ ?

    QObject::property and QQmlPropertyMap already tried...

    Thnx


  • Qt Champions 2017

    1. You need to get the c++ QObject corresponding to QML object.
    2. You need to use the combination of engine.rootObjects() & QObject children() API.
    3. Once you get the object you can fetch each property using QObject property method.
    4. Result of property function will be QVariant. This you need to convert to JSON object & use it.

    Long way...

    Why do you want to do this ?



  • Hi, have you checked the info on this link?
    https://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html

    However, I would not recommend you to do that because what you say goes against the MVC design pattern that QML provides.
    Why do you want to access a QML object property from C++? I would recommend defining a Q_PROPERTY on your C++ side to be able to access it from either C++ and QML:
    https://doc.qt.io/qt-5/properties.html

    Other thing you could do is to create custom signal handling for that object. For example, if you click the object, emit a signal that is catched by your C++ class and perform necessary changes.



  • @dheerendra said in QML & Object.defineProperty:

    You need to get the c++ QObject corresponding to QML object.
    You need to use the combination of engine.rootObjects() & QObject children() API.
    Once you get the object you can fetch each property using QObject property method.
    Result of property function will be QVariant. This you need to convert to JSON object & use it.

    Many thanks for your replay... Qt.bind solved my problem... Your solution is right but long way...

    Thanks again.



  • @rrlopez Many thanks for your replay too... Qt.bind solved my problem...


Log in to reply