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 the javascript var from qml to CPP?
QtWS25 Last Chance

How to pass the javascript var from qml to CPP?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qmljavascript qmlvariableobject
5 Posts 3 Posters 1.9k 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.
  • M Offline
    M Offline
    Mathan M
    wrote on last edited by
    #1

    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.

    raven-worxR 1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

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

      157

      1 Reply Last reply
      1
      • M Mathan M

        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.

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by raven-worx
        #3

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

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        2
        • M Offline
          M Offline
          Mathan M
          wrote on last edited by
          #4

          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?

          1 Reply Last reply
          0
          • p3c0P Offline
            p3c0P Offline
            p3c0
            Moderators
            wrote on last edited by p3c0
            #5

            @Mathan-M Convert it to list using toList

            157

            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