Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to pass non-basic existing Qt type from QML to C++
Forum Updated to NodeBB v4.3 + New Features

How to pass non-basic existing Qt type from QML to C++

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 539 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dalishi
    wrote on last edited by dalishi
    #1

    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.

    JoeCFDJ 1 Reply Last reply
    0
    • D dalishi

      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.

      JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by
      #2

      @dalishi some sample QML code you can learn from
      https://github.com/Esri/arcgis-maps-sdk-samples-qt/tree/main/ArcGISRuntimeSDKQt_QMLSamples

      D 1 Reply Last reply
      0
      • JoeCFDJ JoeCFD

        @dalishi some sample QML code you can learn from
        https://github.com/Esri/arcgis-maps-sdk-samples-qt/tree/main/ArcGISRuntimeSDKQt_QMLSamples

        D Offline
        D Offline
        dalishi
        wrote on last edited by dalishi
        #3

        @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.

        A 1 Reply Last reply
        0
        • D dalishi

          @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.

          A Offline
          A Offline
          Asperamanca
          wrote on last edited by
          #4

          @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.

          D 1 Reply Last reply
          0
          • A Asperamanca

            @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.

            D Offline
            D Offline
            dalishi
            wrote on last edited by dalishi
            #5

            @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.

            A 1 Reply Last reply
            0
            • D dalishi

              @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.

              A Offline
              A Offline
              Asperamanca
              wrote on last edited by
              #6

              @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 Reply Last reply
              0
              • D dalishi has marked this topic as solved on

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved