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 478 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 5 Jun 2023, 09:21 last edited by dalishi 6 May 2023, 09:24
    #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.

    J 1 Reply Last reply 5 Jun 2023, 14:03
    0
    • D dalishi
      5 Jun 2023, 09:21

      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.

      J Offline
      J Offline
      JoeCFD
      wrote on 5 Jun 2023, 14:03 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 6 Jun 2023, 01:59
      0
      • J JoeCFD
        5 Jun 2023, 14:03

        @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 6 Jun 2023, 01:59 last edited by dalishi 6 Jun 2023, 02:00
        #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 6 Jun 2023, 07:01
        0
        • D dalishi
          6 Jun 2023, 01:59

          @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 6 Jun 2023, 07:01 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 6 Jun 2023, 10:26
          0
          • A Asperamanca
            6 Jun 2023, 07:01

            @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 6 Jun 2023, 10:26 last edited by dalishi 6 Jun 2023, 10:27
            #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 6 Jun 2023, 10:44
            0
            • D dalishi
              6 Jun 2023, 10:26

              @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 6 Jun 2023, 10:44 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 8 Jun 2023, 03:05

              1/6

              5 Jun 2023, 09:21

              • Login

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