How to pass the javascript var from qml to CPP?



  • I want to pass the results which stored in javascript var from the main.qml to the writeXML.cpp.
    I can see the count, the searchitems are fetched and want to write in XML format.

    How can I achieve this.

    Code snippet:

    var varResultArray;

    varResultArray = idPortalSearchItems.results[1];

    Query:

    How Can I pass it by Javascript var Ex:
    objHomeController.eveWriteXMLFile(varResultArray ); //Passing an Js variable

    writeXML.h

    What will be the datatype of the argument of the method?
    Ex:
    Q_INVOKABLE bool eveWriteXMLFile(??????? varResultArray ); //What will be the datatype?

    How to access the object varResultArray in cpp file?

    Thanks In advance.


  • Moderators

    @Mathan-M Use QVariant as the argument type in C++ function.
    Then convert that QVariant to your required type using one of the to* methods.


  • Moderators

    @Mathan-M said in How to pass the javascript var from qml to CPP?:

    Q_INVOKABLE bool eveWriteXMLFile(??????? varResultArray ); //What will be the datatype?

    An JS array from QML will be implicitly converted to a QVariantList.
    Read this for more info about type conversion.



  • Hi,

    I used the Qvariant, But still I cannot pass the value [JS array] from the qml.

    Code Snippet:

    in main.qml:

    var varResultArray;
    objHomeController.eveWriteXML(varResultowner);

    in homectrl.h:
    Q_INVOKABLE bool eveWriteXML(QVariant pqvarPortalItemResult);

    in homectrl.cpp:

    bool HomeController::eveWriteXML(QVariant pqvarPortalItemResult)
    {

      QStringList strList = pqvarPortalItemResult.toStringList();
      int intCount= strList .count(); //Count = 0
    

    }

    Am I doing the right thing?


  • Moderators

    @Mathan-M Convert it to list using toList



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