QML & Object.defineProperty
-
wrote on 30 Dec 2019, 22:29 last edited by
Hi;
How to read property of QML object that defined by Object.defineProperty from C++ ?
QObject::property and QQmlPropertyMap already tried...
Thnx
-
- 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.
Long way...
Why do you want to do this ?
-
wrote on 31 Dec 2019, 17:37 last edited by
Hi, have you checked the info on this link?
https://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.htmlHowever, 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.htmlOther 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.
-
- 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.
Long way...
Why do you want to do this ?
wrote on 3 Jan 2020, 22:32 last edited by@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.
-
Hi, have you checked the info on this link?
https://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.htmlHowever, 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.htmlOther 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.
1/5