Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Transfer parameter to C++ by reference from QML
QtWS25 Last Chance

Transfer parameter to C++ by reference from QML

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 2 Posters 2.0k Views
  • 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.
  • W Offline
    W Offline
    wade-J
    wrote on last edited by
    #1

    As I know,QML object will be convert to QVariantMap when transferred to C++ as method parameter, but this is transferred by value and new QVariantMap is created. The performance is not good enough for me.

    What if I want to transfer an object by reference to c++ that the object is created by C++ registered type.

    As example:
    @//MyClass is C++ defined QObject-derived class and register to QML
    qmlRegisterType<MyClass>("com.mycompany.myclass", 1, 0, "MyClass");@

    In QML:
    @var cObj = Qt.createQmlObject('import QtQuick 2.2; import com.mycompany.myclass 1.0;MyClass{}', mwindow, "dynamicMyImageDataCreate");

       //cppObj is a C++ object exposed to QML.
      
       cppObj.foo(cObj);@
    

    In foo(),I want to convert cObj to as local object. foo may some like this:

    @ void foo(QObject* cObj){
           MyClass* mc = (MyClass*)cObj;
           cObj -> x;
    }@
    

    Does Qt support this or is there way I can do this? Does Qt have mechanism to do such thing?

    Thanks for replying.

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      I don't think this is possible. However you can get the reference to cObj in c++ by doing some C++ programming. If you want I can pass that sample to you.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • W Offline
        W Offline
        wade-J
        wrote on last edited by
        #3

        [quote author="Dheerendra" date="1406651314"]I don't think this is possible. However you can get the reference to cObj in c++ by doing some C++ programming. If you want I can pass that sample to you.

        [/quote]

        Since JavaScript could create c++ object and operate it, there should be some way to pass by reference in JavaScript to operate that object.

        I glanced at the code, qv4 engine seems has a struct called "Value" in "qv4value_p.h" to do such thing. I am still looking for the way.

        I appreciate your sample,and this is my email:
        jianwei_sohu@126.com

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          I have sent sample program over the email.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          0
          • W Offline
            W Offline
            wade-J
            wrote on last edited by
            #5

            Yes, I have tried the way as my question. It works. But only for C++ objects as parameters. If js object,such as var o = {...}, we could use QJSValue or QVariantMap to accept.

            1 Reply Last reply
            0

            • Login

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