Results 1 to 3 of 3

Thread: QHash<QString, QVarant> to file

  1. #1
    Join Date
    Nov 2009
    Posts
    68
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded

    Default QHash<QString, QVarant> to file

    I would like to serialize and deserialize a QHash<QString , QVariant> to a file. Is there an easy way to do this?

  2. #2
    Join Date
    Nov 2009
    Posts
    68
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded

    Default Re: QHash<QString, QVarant> to file

    How about something like this:

    Qt Code:
    1. QHash<QString, QVariant> entries;
    2.  
    3. entries.insert("key1", 1);
    4. entries.insert("key2", "seven");
    5. entries.insert("key3", 22.7);
    6.  
    7. QFile fileOut("hashtest.dat");
    8. if (fileOut.open(QIODevice::WriteOnly))
    9. {
    10. QDataStream out(&fileOut);
    11. out.setVersion(QDataStream::Qt_4_6);
    12.  
    13. out << entries;
    14. fileOut.flush();
    15. fileOut.close();
    16. }
    17.  
    18. QHash<QString, QVariant> inputs;
    19.  
    20. QFile fileIn("hashtest.dat");
    21. if (fileIn.open(QIODevice::ReadOnly))
    22. {
    23. QDataStream in(&fileIn);
    24. in.setVersion(QDataStream::Qt_4_6);
    25.  
    26. in >> inputs;
    27.  
    28. fileIn.close();
    29. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QHash<QString, QVarant> to file

    If it works, it's OK right?*





    *Disclaimer: if it works it might still be wrong. It looks OK though.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

Similar Threads

  1. How does QString::qHash() work?
    By Morea in forum Qt Programming
    Replies: 4
    Last Post: 7th September 2009, 09:17
  2. Replies: 3
    Last Post: 28th March 2009, 15:37
  3. Replies: 1
    Last Post: 10th February 2009, 09:42
  4. QDataStream and QHash<QString,QVariant> crash on read
    By ^NyAw^ in forum Qt Programming
    Replies: 1
    Last Post: 15th July 2008, 12:14
  5. Can QHash::capacity() be smaller than QHash::size()?!?
    By iw2nhl in forum Qt Programming
    Replies: 2
    Last Post: 24th August 2007, 01:17

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.