Results 1 to 2 of 2

Thread: Serializing/DeSerializing Nested QHash objects

  1. #1
    Join Date
    Jun 2016
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Serializing/DeSerializing Nested QHash objects

    So im at a bit of a loss and im hoping someone here can help.

    My object being serialized looks like this

    Qt Code:
    1. QHash<QString, QHash<QString, QHash<QString, QVariant>*>*>* hosthash = new QHash<QString, QHash<QString, QHash<QString, QVariant>*>*>();
    To copy to clipboard, switch view to plain text mode 

    Then as my application is running the lowest level QHash<QString,QVariant> gets populated and all is good when it comes time to serialize, where i then nest it into a few more hashes to properly organize all the data.
    At the lowest hash i have a deviceModel that contains all the data i want, which i wrap in another hash whose key differentiates that device from other devices on the system, and then that is wrapped once again in a hash whose key differentiates different systems. Its preferred to keep it this way because it will allow me to easily recreate a nested QTree with all the pertinent information(in theory).

    I wrote two overridden >> operators for datastream to dereference the pointer and pass it into the stream.

    Qt Code:
    1. QDataStream &operator <<(QDataStream &stream, const QHash<QString, QVariant>* deviceModelHash) {
    2. stream << *deviceModelHash;
    3. return stream;
    4. }
    5.  
    6. QDataStream &operator <<(QDataStream &stream, const QHash<QString, QHash<QString, QVariant>*>* deviceHash) {
    7. stream << *deviceHash;
    8. return stream;
    9. }
    To copy to clipboard, switch view to plain text mode 

    All seems well, when i write it to a file it comes out to be 3100ish bytes which seems realistic for how much data im storing.

    But the problem comes with Deserializing. Say i have another hash i want to read into that does not consist of pointers because its not needed when coming back in.

    Qt Code:
    1. QHash<QString, QHash<QString, QHash<QString, QVariant>>> testhash
    To copy to clipboard, switch view to plain text mode 

    How do i get this data back into a workable hash? I assume i need to make some sort datastream >> testHash override but i am confused how to go about doing that. Just doing datastream >> TestHash after opening the stream from file results in an empty testHash. But im at a loss for how to do it manually in the overridden >> function because of how nested the data is. I cant create the top level of the hash without knowing what the 2nd level of the hash is, and i cant create the 2nd level of the hash without having read in the 3rd level of the hash. Hopefully that makes sense?

    Thanks.
    Last edited by jncarlic; 17th June 2016 at 02:22.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Serializing/DeSerializing Nested QHash objects

    The first question you should ask is why you need all those pointers in your structure.
    That makes the function signatures more complicated because then you have references of pointers instead of just the normal references.

    Cheers,
    _

Similar Threads

  1. Nested QHash in range for loop
    By Scope in forum Qt Programming
    Replies: 17
    Last Post: 7th December 2015, 11:17
  2. Serializing a QHash leads to SEGFault
    By gbonnema in forum Newbie
    Replies: 10
    Last Post: 7th January 2015, 18:00
  3. nested QTableView objects
    By Schluchti in forum Qt Programming
    Replies: 0
    Last Post: 8th February 2014, 14:06
  4. Serializing nested objects
    By jiveaxe in forum Newbie
    Replies: 5
    Last Post: 13th December 2013, 17:29
  5. Serializing QHash to a QByteArray
    By PeterThePuter in forum Qt Programming
    Replies: 3
    Last Post: 17th December 2010, 23:19

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.