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. Passing QJsonObject works, but QJsonArray doesn't ?
Forum Updated to NodeBB v4.3 + New Features

Passing QJsonObject works, but QJsonArray doesn't ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 3 Posters 1.2k 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by SPlatten
    #1

    I have a function defined in my class:

    Q_INVOKABLE void log(quint16 uint16Level,const QJsonObject& crobjJSON
                                                 ,const QString& crstrFile
                                                 ,long lngLine);
    

    The above works fine when called from JavaScript:

    xmleng.log(0, objCmds, strThis, 164);
    

    However I need to pass a QJsonArray so I added the prototype:

     Q_INVOKABLE void log(quint16 uint16Level,const QJsonArray& craryJSON
                                                  ,const QString& crstrFile
                                                  ,long lngLine);
    

    And in the JavaScript I defined an array and call the same with:

    xmleng.log(0, aryCmds, strThis, 164);
    

    The array in JavaScript has two elements, but when I debug the C++ with a break point in the function that accepts an array, the array has 0 items.

    [Edit] I just tried modifying the JavaScript to use an object that contains an array:

    objCmds = {"commands":[]};
    objCmds.push(objCmd1);
    objCmds.push(objCmd2);
    

    The JsonObject version of the function is called, but the array is still empty....

    What's going on? It doesn't look like QJsonArray is working at all.

    Kind Regards,
    Sy

    JonBJ 1 Reply Last reply
    0
    • SPlattenS SPlatten

      I have a function defined in my class:

      Q_INVOKABLE void log(quint16 uint16Level,const QJsonObject& crobjJSON
                                                   ,const QString& crstrFile
                                                   ,long lngLine);
      

      The above works fine when called from JavaScript:

      xmleng.log(0, objCmds, strThis, 164);
      

      However I need to pass a QJsonArray so I added the prototype:

       Q_INVOKABLE void log(quint16 uint16Level,const QJsonArray& craryJSON
                                                    ,const QString& crstrFile
                                                    ,long lngLine);
      

      And in the JavaScript I defined an array and call the same with:

      xmleng.log(0, aryCmds, strThis, 164);
      

      The array in JavaScript has two elements, but when I debug the C++ with a break point in the function that accepts an array, the array has 0 items.

      [Edit] I just tried modifying the JavaScript to use an object that contains an array:

      objCmds = {"commands":[]};
      objCmds.push(objCmd1);
      objCmds.push(objCmd2);
      

      The JsonObject version of the function is called, but the array is still empty....

      What's going on? It doesn't look like QJsonArray is working at all.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @SPlatten
      I am getting lost whether you are dealing with JavaScript versus JSON here. But are you sure your code is right? Why do you think those objCmds.push() push anything onto the [] you have? objCmds is not an array, it's an object with an item named commands whose value is an array? I would have expected something more like

      objCmds["commands"].push(...)
      

      ?

      SPlattenS 1 Reply Last reply
      2
      • JonBJ JonB

        @SPlatten
        I am getting lost whether you are dealing with JavaScript versus JSON here. But are you sure your code is right? Why do you think those objCmds.push() push anything onto the [] you have? objCmds is not an array, it's an object with an item named commands whose value is an array? I would have expected something more like

        objCmds["commands"].push(...)
        

        ?

        SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on last edited by SPlatten
        #3

        @JonB Doh, you are perfectly correct, blond moment....however, in my last trial, I did:

        In JavaScript:

        vr aryCmds = [], objCmds = {"commands":aryCmds};		 
        if ( typeof arguments == "object" && arguments.length > 0
          && typeof arguments[0]["IDs"] == "object" ) {
            objCmd["IDs"] = arguments[0]["IDs"];
            aryCmds.push(objCmd);
            aryCmds.push({"command":{"save":objCmd["IDs"]}});
        }
        xmleng.log(0, objCmds, strThis, 164);
        

        xmleng.log is my C++ call, when I break in this function for this call, the debugger shows 0 elements for the array.
        Screenshot 2021-03-21 at 16.43.04.png

        Just to check it wasn't a problem with the reference I also tried:

        var aryCmds = [], objCmds = {};		 
        if ( typeof arguments == "object" && arguments.length > 0
          && typeof arguments[0]["IDs"] == "object" ) {
        	objCmd["IDs"] = arguments[0]["IDs"];
        	aryCmds.push(objCmd);
        	aryCmds.push({"command":{"save":objCmd["IDs"]}});
        }	
        objCmds["comands"] = aryCmds;
        xmleng.log(0, objCmds, strThis, 165);
        

        Still the same.

        Kind Regards,
        Sy

        KroMignonK 1 Reply Last reply
        0
        • SPlattenS SPlatten

          @JonB Doh, you are perfectly correct, blond moment....however, in my last trial, I did:

          In JavaScript:

          vr aryCmds = [], objCmds = {"commands":aryCmds};		 
          if ( typeof arguments == "object" && arguments.length > 0
            && typeof arguments[0]["IDs"] == "object" ) {
              objCmd["IDs"] = arguments[0]["IDs"];
              aryCmds.push(objCmd);
              aryCmds.push({"command":{"save":objCmd["IDs"]}});
          }
          xmleng.log(0, objCmds, strThis, 164);
          

          xmleng.log is my C++ call, when I break in this function for this call, the debugger shows 0 elements for the array.
          Screenshot 2021-03-21 at 16.43.04.png

          Just to check it wasn't a problem with the reference I also tried:

          var aryCmds = [], objCmds = {};		 
          if ( typeof arguments == "object" && arguments.length > 0
            && typeof arguments[0]["IDs"] == "object" ) {
          	objCmd["IDs"] = arguments[0]["IDs"];
          	aryCmds.push(objCmd);
          	aryCmds.push({"command":{"save":objCmd["IDs"]}});
          }	
          objCmds["comands"] = aryCmds;
          xmleng.log(0, objCmds, strThis, 165);
          

          Still the same.

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by KroMignon
          #4

          @SPlatten said in Passing QJsonObject works, but QJsonArray doesn't ?:

          xmleng.log is my C++ call, when I break in this function for this call, the debugger shows 0 elements for the array.

          I can not understand how you expect this to work?!?

          As far as I can understand your code, you are creating an QVariantMap not an QJsonArray.

          I am pretty sure, if you change your function signature to:

          _INVOKABLE void log(quint16 uint16Level,const QVariantMap& crobjJSON
                                                       ,const QString& crstrFile
                                                       ,long lngLine);
          

          ==> https://doc.qt.io/qt-5/qtqml-cppintegration-data.html#qvariantlist-and-qvariantmap-to-javascript-array-and-object

          I don't think QJsonArray is available on QML side.

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          SPlattenS 1 Reply Last reply
          0
          • KroMignonK KroMignon

            @SPlatten said in Passing QJsonObject works, but QJsonArray doesn't ?:

            xmleng.log is my C++ call, when I break in this function for this call, the debugger shows 0 elements for the array.

            I can not understand how you expect this to work?!?

            As far as I can understand your code, you are creating an QVariantMap not an QJsonArray.

            I am pretty sure, if you change your function signature to:

            _INVOKABLE void log(quint16 uint16Level,const QVariantMap& crobjJSON
                                                         ,const QString& crstrFile
                                                         ,long lngLine);
            

            ==> https://doc.qt.io/qt-5/qtqml-cppintegration-data.html#qvariantlist-and-qvariantmap-to-javascript-array-and-object

            I don't think QJsonArray is available on QML side.

            SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on last edited by SPlatten
            #5

            @KroMignon No, here is the actual prototype(s):

            Q_INVOKABLE void log(quint16 uint16Level,const QJsonArray& craryJSON
                                                    ,const QString& crstrFile
                                                    ,long lngLine);
            Q_INVOKABLE void log(quint16 uint16Level,const QJsonObject& crobjJSON
                                                    ,const QString& crstrFile
                                                    ,long lngLine);
            

            Kind Regards,
            Sy

            KroMignonK 2 Replies Last reply
            0
            • SPlattenS SPlatten

              @KroMignon No, here is the actual prototype(s):

              Q_INVOKABLE void log(quint16 uint16Level,const QJsonArray& craryJSON
                                                      ,const QString& crstrFile
                                                      ,long lngLine);
              Q_INVOKABLE void log(quint16 uint16Level,const QJsonObject& crobjJSON
                                                      ,const QString& crstrFile
                                                      ,long lngLine);
              
              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by
              #6

              @SPlatten Sorry, my reply goes out too fast! I have edited it, to avoid noise on this feed.

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              1 Reply Last reply
              0
              • SPlattenS SPlatten

                @KroMignon No, here is the actual prototype(s):

                Q_INVOKABLE void log(quint16 uint16Level,const QJsonArray& craryJSON
                                                        ,const QString& crstrFile
                                                        ,long lngLine);
                Q_INVOKABLE void log(quint16 uint16Level,const QJsonObject& crobjJSON
                                                        ,const QString& crstrFile
                                                        ,long lngLine);
                
                KroMignonK Offline
                KroMignonK Offline
                KroMignon
                wrote on last edited by
                #7

                @SPlatten said in Passing QJsonObject works, but QJsonArray doesn't ?:

                No, here is the actual prototype(s):

                Yes, I saw it, but as I write: I don't think QJsonArray can be created on QML side.

                I found nothing about this in Qt documentation (https://doc.qt.io/qt-5/qtqml-cppintegration-data.html).

                And, for me var objCmds = {} will be converted into a QVariantMap on C++ side

                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                SPlattenS 1 Reply Last reply
                0
                • KroMignonK KroMignon

                  @SPlatten said in Passing QJsonObject works, but QJsonArray doesn't ?:

                  No, here is the actual prototype(s):

                  Yes, I saw it, but as I write: I don't think QJsonArray can be created on QML side.

                  I found nothing about this in Qt documentation (https://doc.qt.io/qt-5/qtqml-cppintegration-data.html).

                  And, for me var objCmds = {} will be converted into a QVariantMap on C++ side

                  SPlattenS Offline
                  SPlattenS Offline
                  SPlatten
                  wrote on last edited by
                  #8

                  @KroMignon , I'm not using QML at all, this is purely JavaScript calling my API routines which are in C++.

                  var objCmds = {} is converted to QJsonObject correctly, but var aryCmds = [] isn't converted to QJsonArray or appears not to be.

                  Kind Regards,
                  Sy

                  SPlattenS JonBJ 2 Replies Last reply
                  0
                  • SPlattenS SPlatten

                    @KroMignon , I'm not using QML at all, this is purely JavaScript calling my API routines which are in C++.

                    var objCmds = {} is converted to QJsonObject correctly, but var aryCmds = [] isn't converted to QJsonArray or appears not to be.

                    SPlattenS Offline
                    SPlattenS Offline
                    SPlatten
                    wrote on last edited by
                    #9

                    @SPlatten, can anyone help?

                    Kind Regards,
                    Sy

                    1 Reply Last reply
                    0
                    • SPlattenS SPlatten

                      @KroMignon , I'm not using QML at all, this is purely JavaScript calling my API routines which are in C++.

                      var objCmds = {} is converted to QJsonObject correctly, but var aryCmds = [] isn't converted to QJsonArray or appears not to be.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #10

                      @SPlatten said in Passing QJsonObject works, but QJsonArray doesn't ?:

                      var objCmds = {} is converted to QJsonObject correctly, but var aryCmds = [] isn't converted to QJsonArray or appears not to be.

                      Your code is so complex. If you want to test this theory, have you tried with a data structure which just has a var aryCmds = [...] in it, and nothing else?

                      SPlattenS 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @SPlatten said in Passing QJsonObject works, but QJsonArray doesn't ?:

                        var objCmds = {} is converted to QJsonObject correctly, but var aryCmds = [] isn't converted to QJsonArray or appears not to be.

                        Your code is so complex. If you want to test this theory, have you tried with a data structure which just has a var aryCmds = [...] in it, and nothing else?

                        SPlattenS Offline
                        SPlattenS Offline
                        SPlatten
                        wrote on last edited by
                        #11

                        @JonB, the code isn't complex, looks very simple, I've tried various different and "simple" versions just passing an array, although the array has at least one element when the C++ routine is called, when it gets to the routine the array is empty.

                        Kind Regards,
                        Sy

                        KroMignonK 1 Reply Last reply
                        0
                        • SPlattenS SPlatten

                          @JonB, the code isn't complex, looks very simple, I've tried various different and "simple" versions just passing an array, although the array has at least one element when the C++ routine is called, when it gets to the routine the array is empty.

                          KroMignonK Offline
                          KroMignonK Offline
                          KroMignon
                          wrote on last edited by KroMignon
                          #12

                          @SPlatten said in Passing QJsonObject works, but QJsonArray doesn't ?:

                          the code isn't complex, looks very simple, I've tried various different and "simple" versions just passing an array, although the array has at least one element when the C++ routine is called, when it gets to the routine the array is empty.

                          What I don't understand is why you expect that Qt/C++ should implicitly convert a Java array into QJsonArray?

                          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                          SPlattenS 1 Reply Last reply
                          0
                          • KroMignonK KroMignon

                            @SPlatten said in Passing QJsonObject works, but QJsonArray doesn't ?:

                            the code isn't complex, looks very simple, I've tried various different and "simple" versions just passing an array, although the array has at least one element when the C++ routine is called, when it gets to the routine the array is empty.

                            What I don't understand is why you expect that Qt/C++ should implicitly convert a Java array into QJsonArray?

                            SPlattenS Offline
                            SPlattenS Offline
                            SPlatten
                            wrote on last edited by SPlatten
                            #13

                            @KroMignon , it isn't Java, its JavaScript and I expect the Qt API to convert the array because it does exactly that for a JavaScript object. What is the point of offering only half a solution? If JavaScript objects are processed then arrays should also be processed.

                            Kind Regards,
                            Sy

                            KroMignonK 1 Reply Last reply
                            0
                            • SPlattenS SPlatten

                              @KroMignon , it isn't Java, its JavaScript and I expect the Qt API to convert the array because it does exactly that for a JavaScript object. What is the point of offering only half a solution? If JavaScript objects are processed then arrays should also be processed.

                              KroMignonK Offline
                              KroMignonK Offline
                              KroMignon
                              wrote on last edited by
                              #14

                              @SPlatten said in Passing QJsonObject works, but QJsonArray doesn't ?:

                              it isn't Java, its JavaScript and I expect the Qt API to convert the array because it does exactly that for a JavaScript object. What is the point of offering only half a solution? If JavaScript objects are processed then arrays should also be processed.

                              Yes, you are right it is JavaScript, and JavaScript is not known as strongly typed language.
                              There are many implicit conversion done, so awaiting that this is just working seems not really serious to me.

                              I wish you good luke!

                              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                              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