How to pass non-basic existing Qt type from QML to C++
-
wrote on 5 Jun 2023, 09:21 last edited by dalishi 6 May 2023, 09:24
Hi, I'm using QtLocation and want to pass some QML types like Route, I found the corresponding C++ type QGeoRoute, can I directly pass the data of QML Route type to QGeoRoute in C++?
In C++, I define
class RouteInterface : public QObject { ... Q_INVOKABLE void publishRoute(const QGeoRoute &route) {...} ... }
When I pass the data directly, like in component.qml:
iface.publishRoute(routeModel.get(0)) // routeModel.get(0) is of type Route
I get:
Error: Unknown method parameter type: QGeoRoute
I try to register the QGeoRoute type with QML in main.cpp:
qRegisterMetaType<QGeoRoute>("QGeoRoute");
I get:
"Could not convert argument 0 at" "publishRoute@qrc:/component.qml:195" "expression for onStatusChanged@qrc:/component.qml:218" "Passing incompatible arguments to C++ functions from JavaScript is dangerous and deprecated." "This will throw a JavaScript TypeError in future releases of Qt!"
Is there a way I can pass data of this type?
Thanks.
-
Hi, I'm using QtLocation and want to pass some QML types like Route, I found the corresponding C++ type QGeoRoute, can I directly pass the data of QML Route type to QGeoRoute in C++?
In C++, I define
class RouteInterface : public QObject { ... Q_INVOKABLE void publishRoute(const QGeoRoute &route) {...} ... }
When I pass the data directly, like in component.qml:
iface.publishRoute(routeModel.get(0)) // routeModel.get(0) is of type Route
I get:
Error: Unknown method parameter type: QGeoRoute
I try to register the QGeoRoute type with QML in main.cpp:
qRegisterMetaType<QGeoRoute>("QGeoRoute");
I get:
"Could not convert argument 0 at" "publishRoute@qrc:/component.qml:195" "expression for onStatusChanged@qrc:/component.qml:218" "Passing incompatible arguments to C++ functions from JavaScript is dangerous and deprecated." "This will throw a JavaScript TypeError in future releases of Qt!"
Is there a way I can pass data of this type?
Thanks.
wrote on 5 Jun 2023, 14:03 last edited by@dalishi some sample QML code you can learn from
https://github.com/Esri/arcgis-maps-sdk-samples-qt/tree/main/ArcGISRuntimeSDKQt_QMLSamples -
@dalishi some sample QML code you can learn from
https://github.com/Esri/arcgis-maps-sdk-samples-qt/tree/main/ArcGISRuntimeSDKQt_QMLSampleswrote on 6 Jun 2023, 01:59 last edited by dalishi 6 Jun 2023, 02:00@JoeCFD Hi thanks for the post. In the example NavigateRoute I found a basic data exchange (QString) which is no issues as Qt automatically converts when string type param is passed. Unfortunately I did not manage to find examples showing the data exchange like in my case.
-
@JoeCFD Hi thanks for the post. In the example NavigateRoute I found a basic data exchange (QString) which is no issues as Qt automatically converts when string type param is passed. Unfortunately I did not manage to find examples showing the data exchange like in my case.
wrote on 6 Jun 2023, 07:01 last edited by@dalishi
QGeoRoute probably has no 1:1 representation in QML. What do you want to do with it on the QML side?If all you want to do is pass it from C++ to QML and back, you can make sure it is registered as a metatype and use QVariant as interface towards QML.
-
@dalishi
QGeoRoute probably has no 1:1 representation in QML. What do you want to do with it on the QML side?If all you want to do is pass it from C++ to QML and back, you can make sure it is registered as a metatype and use QVariant as interface towards QML.
wrote on 6 Jun 2023, 10:26 last edited by dalishi 6 Jun 2023, 10:27@Asperamanca Hi I want to pass Route in QML to C++. I thought I could directly pass to QGeoRoute in C++ but as you also mentioned, QML Route and QGeoRoute are not 1:1. I dont know how to pass QML Route data to C++. Probably I have to split it into basic types and pass to C++ one by one.
-
@Asperamanca Hi I want to pass Route in QML to C++. I thought I could directly pass to QGeoRoute in C++ but as you also mentioned, QML Route and QGeoRoute are not 1:1. I dont know how to pass QML Route data to C++. Probably I have to split it into basic types and pass to C++ one by one.
wrote on 6 Jun 2023, 10:44 last edited by@dalishi I haven't worked with that module, so I don't know how the QML data is structured. Typically, a QML type is implemented in terms of a (private API) C++ class, and maybe you can look it up in Qt source code.
-
1/6