Results 1 to 6 of 6

Thread: Serialization and inheritance using Qt

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    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...

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 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.


  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

    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
  •  
Qt is a trademark of The Qt Company.