How to pass non-basic existing Qt type from QML to C++
-
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 RouteI get:
Error: Unknown method parameter type: QGeoRouteI 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 RouteI get:
Error: Unknown method parameter type: QGeoRouteI 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.
-
@dalishi some sample QML code you can learn from
https://github.com/Esri/arcgis-maps-sdk-samples-qt/tree/main/ArcGISRuntimeSDKQt_QMLSamples@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.
@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.
@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.
@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.
-
D dalishi has marked this topic as solved on