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. QJsonObject: Modify field value
QtWS25 Last Chance

QJsonObject: Modify field value

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 5 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.
  • F Offline
    F Offline
    fem_dev
    wrote on last edited by fem_dev
    #1

    I have a QJsonObject like this:

    QJsonObject* json = new QJsonObject();
    _input_data->insert("Name", QJsonValue::fromVariant("Paul"));
    

    Now I would like to change the value, using:

    _input_data["Name"] = "Nyck";
    

    But I got a compilation error:

    error: array subscript is not an integer
    

    How can I change the value?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      First - why do you create the object on the heap instead simply on the stack?
      And then - since _input_data is a pointer, _input_data["Name"] will not do what you expect, you're looking for (*_input_data)["Name"]

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      F 1 Reply Last reply
      5
      • Christian EhrlicherC Christian Ehrlicher

        First - why do you create the object on the heap instead simply on the stack?
        And then - since _input_data is a pointer, _input_data["Name"] will not do what you expect, you're looking for (*_input_data)["Name"]

        F Offline
        F Offline
        fem_dev
        wrote on last edited by
        #3

        @christian-ehrlicher thanks!

        I'm creating the object on the heap because this QJsonObject could be bigger then a stack size (1MB).

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @fem_dev said in QJsonObject: Modify field value:

          I'm creating the object on the heap because this QJsonObject could be bigger then a stack size (1MB).

          Although the object is created on the stack, the internal data of it won't be on the stack so it's not needed to create the object on the heap (and get all the drawbacks you're seeing).

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          F JKSHJ 2 Replies Last reply
          7
          • Christian EhrlicherC Christian Ehrlicher

            @fem_dev said in QJsonObject: Modify field value:

            I'm creating the object on the heap because this QJsonObject could be bigger then a stack size (1MB).

            Although the object is created on the stack, the internal data of it won't be on the stack so it's not needed to create the object on the heap (and get all the drawbacks you're seeing).

            F Offline
            F Offline
            fem_dev
            wrote on last edited by
            #5

            @christian-ehrlicher thank you so much for this great information! I didn't know that!

            1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              @fem_dev said in QJsonObject: Modify field value:

              I'm creating the object on the heap because this QJsonObject could be bigger then a stack size (1MB).

              Although the object is created on the stack, the internal data of it won't be on the stack so it's not needed to create the object on the heap (and get all the drawbacks you're seeing).

              JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by
              #6

              @christian-ehrlicher said in QJsonObject: Modify field value:

              Although the object is created on the stack, the internal data of it won't be on the stack so it's not needed to create the object on the heap (and get all the drawbacks you're seeing).

              To add to @Christian-Ehrlicher, most of Qt's "data" objects should not be created on the heap, for example:

              • QJsonObject
              • QVector
              • QString
              • QImage
              • QIcon

              They are also very cheap to copy, so functions should return them by-value (not by-pointer or by-reference). Read https://doc.qt.io/qt-5/implicit-sharing.html

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              F 2 Replies Last reply
              5
              • JKSHJ JKSH

                @christian-ehrlicher said in QJsonObject: Modify field value:

                Although the object is created on the stack, the internal data of it won't be on the stack so it's not needed to create the object on the heap (and get all the drawbacks you're seeing).

                To add to @Christian-Ehrlicher, most of Qt's "data" objects should not be created on the heap, for example:

                • QJsonObject
                • QVector
                • QString
                • QImage
                • QIcon

                They are also very cheap to copy, so functions should return them by-value (not by-pointer or by-reference). Read https://doc.qt.io/qt-5/implicit-sharing.html

                F Offline
                F Offline
                fem_dev
                wrote on last edited by
                #7

                @JKSH woww...this is new for me: "Implicity sharing"...

                I read this Qt article, but I would like to confirm if I understant it in the right way.

                So, using Qt 5 framework, if I have some code like:

                void main()
                {
                    QJsonObject json;
                
                    // Fill this JSON object with some data...
                
                    // Send data to a function:
                    my_func(json)
                }
                

                This my_func() should be ALWAYS like

                void my_func(QJsonObject x)
                

                and NO in this tradicional way like:

                void my_func(QJsonObject &x)
                

                I said ALWAYS because if my_func() do not modify the json input argument, it will be passed by reference, like QJsonObject &x.
                But if the my_func() modify the json input argument, it will be passed by value, like QJsonObject x.

                Is this afirmation correct?

                aha_1980A 1 Reply Last reply
                0
                • F fem_dev

                  @JKSH woww...this is new for me: "Implicity sharing"...

                  I read this Qt article, but I would like to confirm if I understant it in the right way.

                  So, using Qt 5 framework, if I have some code like:

                  void main()
                  {
                      QJsonObject json;
                  
                      // Fill this JSON object with some data...
                  
                      // Send data to a function:
                      my_func(json)
                  }
                  

                  This my_func() should be ALWAYS like

                  void my_func(QJsonObject x)
                  

                  and NO in this tradicional way like:

                  void my_func(QJsonObject &x)
                  

                  I said ALWAYS because if my_func() do not modify the json input argument, it will be passed by reference, like QJsonObject &x.
                  But if the my_func() modify the json input argument, it will be passed by value, like QJsonObject x.

                  Is this afirmation correct?

                  aha_1980A Offline
                  aha_1980A Offline
                  aha_1980
                  Lifetime Qt Champion
                  wrote on last edited by aha_1980
                  #8

                  Hi @fem_dev,

                  The general rule of thumb is: if the size of your object is <= 16 bytes, then pass by value: void my_func(Object x), otherwise pass by const reference: void my_func(const Object &x). Just to be clear: that's the object's size, not the data's size! Most Qt objects are not much more than a pointer to private data + a few bytes for organisation. That's why it's cheap to pass them by value.

                  If you need to modify the object within the function, you have to pass the object as pointer or reference: void my_func(Object &x) or void my_func(Object *x).

                  Note that Qt (and personally me) prefer the pointer syntax, as this makes it more obvious the object is modified in the function:

                  void my_func(Object *o);
                  
                  void test()
                  {
                    Object x;
                    my_func(&x);
                  }
                  

                  I said ALWAYS because if my_func() do not modify the json input argument, it will be passed by reference, like QJsonObject &x.

                  No, you pass by const-ref or by value here.

                  But if the my_func() modify the json input argument, it will be passed by value, like QJsonObject x.

                  If you pass by value, you can modify the object within my_func, but that will not change it outside my_func(). Not sure if I understand what you mean here.

                  Regards

                  Qt has to stay free or it will die.

                  1 Reply Last reply
                  3
                  • JKSHJ JKSH

                    @christian-ehrlicher said in QJsonObject: Modify field value:

                    Although the object is created on the stack, the internal data of it won't be on the stack so it's not needed to create the object on the heap (and get all the drawbacks you're seeing).

                    To add to @Christian-Ehrlicher, most of Qt's "data" objects should not be created on the heap, for example:

                    • QJsonObject
                    • QVector
                    • QString
                    • QImage
                    • QIcon

                    They are also very cheap to copy, so functions should return them by-value (not by-pointer or by-reference). Read https://doc.qt.io/qt-5/implicit-sharing.html

                    F Offline
                    F Offline
                    fem_dev
                    wrote on last edited by
                    #9

                    @aha_1980 said in QJsonObject: Modify field value:

                    Not sure if I understand what you mean here.

                    Yes @aha_1980, I undertood you and I agree with it!

                    My confusion is about the @Christian-Ehrlicher explanation here:

                    @JKSH said in QJsonObject: Modify field value:

                    so functions should return them by-value (not by-pointer or by-reference).

                    So, in this example below, I want to return a BIG QJsonObject. What should I do?

                    Example 1:

                    void my_func(const QJsonObject &json);
                    
                    void main() 
                    {
                        // Create a QJsonObject outside the function:
                        QJsonObject big_json;
                    
                        // Fill this object with data...
                    
                        // Send data to the function:
                        my_func(big_json)
                    
                        // Use the modified QJsonObject...
                    }
                    

                    Example 2:

                    QJsonObject my_func(void);
                    
                    void main() 
                    {
                        // The QJsonObject will be created inside of 'my_func' and will be returned by value:
                        QJsonObject big_json = my_func();
                    
                        // Use the QJsonObject...
                    }
                    

                    My first idea is use the "Example 1" to avoid copying data, but when I read the @Christian-Ehrlicher response, I don't know which case I should use.

                    Could you help me to understant the @Christian-Ehrlicher afirmation?

                    JonBJ 1 Reply Last reply
                    0
                    • F fem_dev

                      @aha_1980 said in QJsonObject: Modify field value:

                      Not sure if I understand what you mean here.

                      Yes @aha_1980, I undertood you and I agree with it!

                      My confusion is about the @Christian-Ehrlicher explanation here:

                      @JKSH said in QJsonObject: Modify field value:

                      so functions should return them by-value (not by-pointer or by-reference).

                      So, in this example below, I want to return a BIG QJsonObject. What should I do?

                      Example 1:

                      void my_func(const QJsonObject &json);
                      
                      void main() 
                      {
                          // Create a QJsonObject outside the function:
                          QJsonObject big_json;
                      
                          // Fill this object with data...
                      
                          // Send data to the function:
                          my_func(big_json)
                      
                          // Use the modified QJsonObject...
                      }
                      

                      Example 2:

                      QJsonObject my_func(void);
                      
                      void main() 
                      {
                          // The QJsonObject will be created inside of 'my_func' and will be returned by value:
                          QJsonObject big_json = my_func();
                      
                          // Use the QJsonObject...
                      }
                      

                      My first idea is use the "Example 1" to avoid copying data, but when I read the @Christian-Ehrlicher response, I don't know which case I should use.

                      Could you help me to understant the @Christian-Ehrlicher afirmation?

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

                      @fem_dev
                      It would not matter which of these two you use.

                      This is because there never is a "big" JSON object. QJsonObject is always small, because the JSON data is not stored actually in QJsonObject, instead that just holds a pointer to the data elsewhere. This is why @JKSH is saying QJsonObject is "cheap to copy" and that you can create it on the stack instead of the heap, since it's small.

                      EDIT
                      P.S.
                      For you or anyone else reading this. Do read @JKSH's link to https://doc.qt.io/qt-5/implicit-sharing.html. You find QJsonObject listed among the Qt classes which utilise it.

                      In summary this means that that your actual "big data" of the JSON content is stored internally by Qt elsewhere in some "shared object data table" instead of in your QJsonObject, and that just has a "conceptual pointer" to the data.

                      The data is reference counted, which means that (a) Qt looks after disposing it once the final reference to it in code goes out of scope, so you don't have to worry about that, and (b) if code "copies" it (e.g. a QJsonObject) all Qt does internally is increment the reference count to that data blob, so it's very fast and does not cost extra memory. If you only access the data in the copy for read it stays "cheap" like that; if you write to the data then only at that instant does Qt make a copy of the data to a fresh area in the "shared table". So it's pretty efficient!

                      F 1 Reply Last reply
                      3
                      • JonBJ JonB

                        @fem_dev
                        It would not matter which of these two you use.

                        This is because there never is a "big" JSON object. QJsonObject is always small, because the JSON data is not stored actually in QJsonObject, instead that just holds a pointer to the data elsewhere. This is why @JKSH is saying QJsonObject is "cheap to copy" and that you can create it on the stack instead of the heap, since it's small.

                        EDIT
                        P.S.
                        For you or anyone else reading this. Do read @JKSH's link to https://doc.qt.io/qt-5/implicit-sharing.html. You find QJsonObject listed among the Qt classes which utilise it.

                        In summary this means that that your actual "big data" of the JSON content is stored internally by Qt elsewhere in some "shared object data table" instead of in your QJsonObject, and that just has a "conceptual pointer" to the data.

                        The data is reference counted, which means that (a) Qt looks after disposing it once the final reference to it in code goes out of scope, so you don't have to worry about that, and (b) if code "copies" it (e.g. a QJsonObject) all Qt does internally is increment the reference count to that data blob, so it's very fast and does not cost extra memory. If you only access the data in the copy for read it stays "cheap" like that; if you write to the data then only at that instant does Qt make a copy of the data to a fresh area in the "shared table". So it's pretty efficient!

                        F Offline
                        F Offline
                        fem_dev
                        wrote on last edited by
                        #11

                        @JonB thank you! Now I got it!

                        1 Reply Last reply
                        1

                        • Login

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