Results 1 to 6 of 6

Thread: Serialization and inheritance using Qt

  1. #1
    Join Date
    Dec 2009
    Posts
    128
    Thanks
    7
    Thanked 14 Times in 14 Posts
    Platforms
    Unix/X11 Windows

    Default Serialization and inheritance using Qt

    I have an inheritance tree like :

    Qt Code:
    1. BaseClass : QObject {
    2. Q_OBJECT
    3. // etc..
    4. } ;
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. ChildClass1 : BaseClass {
    2. Q_OBJECT
    3. // etc..
    4. } ;
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. ChildClass2 : BaseClass {
    2. Q_OBJECT
    3. // etc..
    4. } ;
    To copy to clipboard, switch view to plain text mode 

    And for each class I declared and defined default constructor, copy constructor, assignment operator overload, and friend << and >> operators for QDataStream support.

    Now, I have a list of BaseClass pointers used as follow :
    Qt Code:
    1. void saveData(QDataStream & stream)
    2. {
    3. QList<BaseClass*> objList ;
    4. objList.append( new ChildClass1() ) ;
    5. objList.append( new ChildClass2() ) ;
    6. stream << objList.length() ;
    7. foreach(BaseClass *obj, objList)
    8. {
    9. stream << (*obj) ;
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    So when I serialize this list, how do I make sure it's actually ChildClass1 or ChildClass2 instances beeing serialized ? stream operators are non-member functions, so they cannot be virtual.

    The same way when I de-serialize objects, (I must be blind but) I'm stuck with the following :
    Qt Code:
    1. void readData(QDataStream & stream)
    2. {
    3. int i, count ;
    4. QList<BaseClass*> someList ;
    5. stream >> count ;
    6. for(i=0; i<count; i++)
    7. {
    8. stream >> ...// now what do I do here ?
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    Long story short, how do I combine Qt serialization and class inheritance ?

    Any pointer would be greatly appreciated

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Serialization and inheritance using Qt

    The object list in your saveData function should not be needed. QObject keeps a list for you already. Just set the correct parent when creating a child. This makes the code a bit nicer.

    As for the serialization. The key is to understand which data belongs to which type of class and which item is a parent or child.
    I personally like xml for this. Example:

    When I have the following parent/child tree:

    Qt Code:
    1. ParentItem1 of Type1
    2. ChildItem1 of Type2
    3. ChildItem2 of Type3
    To copy to clipboard, switch view to plain text mode 

    I can save the data like this
    Qt Code:
    1. <item type="Type1" value1="someValueSpecificForType1Objects">
    2. <item type="Type2" value1="someValueSpecificForType2Objects"/>
    3. <item type="Type3" value1="somethingElseEtc"/>
    4. </item>
    To copy to clipboard, switch view to plain text mode 
    The style of the xml tree is of course personal taste.

    Now, to read this structure back pass the data to the base class and let it create its childs and set the data and pass along the information destined for the children to the child objects so they can do the same.
    You also might want to create a factory if the base class is not always of the same type.

    Edit: this is of course without a qdatastream.
    Note that a QDataStream isn't very flexible. You'll probably need a lot of support code to read and create the child objects (decoding) while it would be trivial with a xml reader/writer
    Last edited by tbscope; 16th January 2011 at 12:23.

  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Serialization and inheritance using Qt

    So when I serialize this list, how do I make sure it's actually ChildClass1 or ChildClass2 instances beeing serialized ?
    one way is to have the base class declare an 'm_type' variable (an enum would be convenient) that the inheriting classes must define - which you then can ask for and based on the type make the correct cast.
    Once the pointer is casted, the correct stream operator will be used automatically.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  4. #4
    Join Date
    Dec 2009
    Posts
    128
    Thanks
    7
    Thanked 14 Times in 14 Posts
    Platforms
    Unix/X11 Windows

    Default Re: Serialization and inheritance using Qt

    @tbscope : thank you for your suggestions. However, I want to keep the QDataStream support; if i'm right it will also open me doors of drag/drop support

    @high_flyer : I do have this system, so I will use the solution to switch on object->type() and instanciate DerivedClass accordingly. It's inelegant but if it works...

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Serialization and inheritance using Qt

    Quote Originally Posted by totem View Post
    @tbscope : thank you for your suggestions. However, I want to keep the QDataStream support; if i'm right it will also open me doors of drag/drop support
    Using QDataStream instead of any other serialization form doesn't help in any way to handle drag&drop. In either case you end up with a block of bytes, it's just a matter of which mechanism populates the blob but nothing happens on its own, you have to write code that does it yourself.

    If you want to use QDataStream in this specific case then your base-class has to know all its potential subclasses and handle conversions between them and itself. This is of course possible but it's quite a lot of work so if you don't feel comfortable with the topic, I wouldn't recommend this approach.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Dec 2009
    Posts
    128
    Thanks
    7
    Thanked 14 Times in 14 Posts
    Platforms
    Unix/X11 Windows

    Default Re: Serialization and inheritance using Qt

    Quote Originally Posted by wysota View Post
    Using QDataStream instead of any other serialization form doesn't help in any way to handle drag&drop
    Yes I'm sorry. I meant I want to be able to use these classes with qRegisterMetaTypeStreamOperators(), I use it for drag/drop.

    Quote Originally Posted by wysota View Post
    If you want to use QDataStream in this specific case then your base-class has to know all its potential subclasses
    My project is not that huge, it's a matter of a dozen classes for now.
    I was just wondering if there existed elegant C++/Qt solution with QDataStream serialization and inheritance.

    Thank you all for your answers

Similar Threads

  1. Class serialization
    By matulik in forum Qt Programming
    Replies: 6
    Last Post: 13th December 2010, 12:17
  2. Serialization of QTextEdit
    By naghekyan in forum Qt Programming
    Replies: 1
    Last Post: 16th August 2010, 06:02
  3. QDataStream and serialization
    By pdoria in forum Qt Programming
    Replies: 5
    Last Post: 11th November 2009, 09:42
  4. XML Serialization of Qt Objects
    By sasi in forum Qt Programming
    Replies: 1
    Last Post: 29th June 2009, 19:25
  5. Serialization
    By donmorr in forum Qt Programming
    Replies: 4
    Last Post: 16th November 2006, 13:51

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.