Transfer parameter to C++ by reference from QML



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



  • 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 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



  • I have sent sample program over the email.



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.