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 do I do qobject2variant in a nested QObject container?
Forum Updated to NodeBB v4.3 + New Features

How do I do qobject2variant in a nested QObject container?

Scheduled Pinned Locked Moved General and Desktop
12 Posts 4 Posters 7.1k 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.
  • R Offline
    R Offline
    raulgd
    wrote on last edited by
    #3

    The problem with QObject pointers is having to destroy them myself right? I would have to use QPointer or something like that so I don't get memory leaks.
    But if I try QList<QPointer<Person> >, I still get the same error, so how would I use smart pointers in my code?

    Raul Guerrero
    http://jimi.mx

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #4

      [quote author="Raul" date="1329789941"]The problem with QObject pointers is having to destroy them myself right?[/quote]
      It depends. If they have a parent, they are automatically destroyed as soon as the parent is destroyed. So if you pass the Personnel object as the parent to Person objects you add to the QList they are automatically destroyed as soon as the Personnel object is destroyed.

      [quote author="Raul" date="1329789941"]I would have to use QPointer or something like that so I don't get memory leaks.[/quote]
      No, you just have to <code>delete</code> them, for example as you take them out of the list or in the destructor.

      Smart pointers can do this for you (semi-)automatically, but they are neither needed nor a guarantee that your application doesn't leak memory.

      In addition, QPointer won't delete the object for you (it just ensures that it isn't dangling). QSharedPointer or QScopedPointer is what you are looking for.

      [quote author="Raul" date="1329789941"]But if I try QList<QPointer<Person> >, I still get the same error, so how would I use smart pointers in my code?[/quote]
      Well, this should work.

      1 Reply Last reply
      0
      • R Offline
        R Offline
        raulgd
        wrote on last edited by
        #5

        Ok so if I create personnel, and set it as personList parent, and then each person in the list as personnel children as well, that would mean that all of them would get automatically destroyed right?

        That way I don't have to worry about memory leaks, I'll do some tests on it and let you know how goes.

        Thanks!

        Raul Guerrero
        http://jimi.mx

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lgeyer
          wrote on last edited by
          #6

          You can't set a parent for QList, as it doesn't inherit QObject, but you don't need to, because as a member of Personnel it is automatically destructed along with the rest of the class anyways. All you have to do is to pass Personnel as parent to Person.

          Alternatively (or additionally) you can iterate over the list passed to setPersonList() and explicitly set the parent for each Person to this (so their relationship is guaranteed).
          @
          int main(int argc, char argv[])
          {
          ...
          Personnel
          personnel = new Personnel;

          QList<Person*> personList;
          personList.append(new Person(personnel));
          personList.append(new Person(personnel));
          personList.append(new Person(personnel));
          
          personnel->setPersonList(personList);
          ...
          delete personnel; // personnel is deleted
                            // personnel.mPersonList is deleted 
                            //    because it is a member of personnel
                            // every Person in personnel.mPersonList is deleted 
                            //    because their parent (personnel) is deleted
          

          }
          @
          An alternative would be to just delete the objects stored in mPersonList in the destructor of Personnel.
          @
          Personnel::~Personnel()
          {
          qDeleteAll(mPersonList.begin(), mPersonList.end()); // walks through the list and
          // calls delete for every member
          }
          @
          The most important things is that you have a well-defined ownership, which means you have to define who owns the Person objects and is therefor responsible for deleting.

          This could be either the object who created the Person objects - main() in our example - or it is the object managing the Person objects - personnel in our example. Both versions have their advantages and disadvantages. For example, #1 enables you to pass the same list of objects to multiple Personnel objects, #2 ensures that the objects are deleted when their "managing class" is deleted.

          It is just a matter of definition. Make sure you have one and you document it.

          1 Reply Last reply
          0
          • R Offline
            R Offline
            raulgd
            wrote on last edited by
            #7

            Wow great explanation! thanks a lot!, so then, from what you say, also seeing that QList is not a QObject, calling object2variant wouldn't actually convert QList to a QVariantList and all of the persons inside the QList wouldn't be converted to a qvariant?

            If that is so then, It would be better for me to learn more about QObject and it's metaobject model, and implement some sort of memento pattern for serialization instead of trying to do it through a qvariant?

            Raul Guerrero
            http://jimi.mx

            1 Reply Last reply
            0
            • L Offline
              L Offline
              lgeyer
              wrote on last edited by
              #8

              Just let me get this right: We are talking about QJson here and you want to serialize a Personnel object using QObjectHelper::qobject2qvariant to either use it as a memento or to convert it to JSON later on.

              I don't think QJson supports user datatypes (like Person*), so you will either have to add support for it to QJson or you create your own serialization.

              And learning about QObject and the metaobject system is always a good idea ;-)

              1 Reply Last reply
              0
              • R Offline
                R Offline
                raulgd
                wrote on last edited by
                #9

                I'm actually using XmlStream rather than QJson... yes QJson could work as a JSON serializer, but what I want to achieve is to first set a canonical object based on QVariant that could be serialized or deserialized into any format, either XML, JSON, or whatever codec I decide to use or implement.

                Raul Guerrero
                http://jimi.mx

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  Jaswinder
                  wrote on last edited by
                  #10

                  hi,
                  Can i show above model to qml. i tried but i am unable to display inner class element. have any idea about to print this in qml.
                  Thanks in advance.

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    raulgd
                    wrote on last edited by
                    #11

                    Sorry, I wouldn't know how to do something like that in QML.

                    But still, I think it would be better practice if you did this kind of logic in C++, and then just pass the object to QML back and forth, because QML is not designed to do this kind of work, QML is mostly UI related only.

                    Raul Guerrero
                    http://jimi.mx

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      Jaswinder
                      wrote on last edited by
                      #12

                      Hi Raul,
                      I created this logic in C++ (object).
                      and now i want to pass this object to nested list view.
                      see this post

                      http://qt-project.org/forums/viewthread/19526/

                      Any Idea about this

                      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