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?
Forum Updated to NodeBB v4.3 + New Features

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 1 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.
  • M Offline
    M Offline
    Mathan M
    wrote on 26 Sept 2016, 10:42 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.

    R 1 Reply Last reply 26 Sept 2016, 11:16
    0
    • P Offline
      P Offline
      p3c0
      Moderators
      wrote on 26 Sept 2016, 10:51 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
        26 Sept 2016, 10:42

        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.

        R Offline
        R Offline
        raven-worx
        Moderators
        wrote on 26 Sept 2016, 11:16 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 26 Sept 2016, 14:46 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
          • P Offline
            P Offline
            p3c0
            Moderators
            wrote on 26 Sept 2016, 15:57 last edited by p3c0
            #5

            @Mathan-M Convert it to list using toList

            157

            1 Reply Last reply
            0

            3/5

            26 Sept 2016, 11:16

            • Login

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