Results 1 to 4 of 4

Thread: Serializing QHash to a QByteArray

  1. #1
    Join Date
    Oct 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Serializing QHash to a QByteArray

    Hey all,

    I am having some trouble serializing a QHash. Following this snippit I attempted this:

    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <QHash>
    3. #include <QVariant>
    4. #include <QDebug>
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8.  
    9. QHash<QString,QVariant> hash;
    10. hash.insert("Key1",1);
    11. hash.insert("Key2","thing2");
    12.  
    13. QDataStream ds(&ba, QIODevice::WriteOnly);
    14. ds << hash;
    15. qDebug() << ds;
    16.  
    17. }
    To copy to clipboard, switch view to plain text mode 

    However, when I run it, qDebug() spits this out:
    Qt Code:
    1. QIODevice::read: WriteOnly device
    2. QIODevice::read: WriteOnly device
    3. QIODevice::read: WriteOnly device
    To copy to clipboard, switch view to plain text mode 

    What am I doing wrong?

    Thanks!
    -J

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

    Default Re: Serializing QHash to a QByteArray

    Did you read your code? ds is defined as WriteOnly. You are then trying to output it to qDebug(). You cannot do that. One could even wonder how you would expect it to be output to qDebug()...
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  3. #3
    Join Date
    Oct 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Serializing QHash to a QByteArray

    **Facepalm** Thanks!

    the QByteArray ba is still empty however:

    Qt Code:
    1. qDebug() << ba.isEmpty()
    To copy to clipboard, switch view to plain text mode 
    gives true.

  4. #4
    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: Serializing QHash to a QByteArray

    Maybe the data isn't flushed to the byte array yet? See if this works:
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <QHash>
    3. #include <QVariant>
    4. #include <QDebug>
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8.  
    9. QHash<QString,QVariant> hash;
    10. hash.insert("Key1",1);
    11. hash.insert("Key2","thing2");
    12.  
    13. {
    14. QDataStream ds(&ba, QIODevice::WriteOnly);
    15. ds << hash;
    16. } // here the stream gets destroyed so it has to flush whatever it caches
    17. qDebug() << ba;
    18. }
    To copy to clipboard, switch view to plain text mode 
    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.


Similar Threads

  1. Serializing QImage
    By lukass in forum Newbie
    Replies: 1
    Last Post: 26th October 2010, 18:58
  2. Replies: 9
    Last Post: 25th July 2009, 13:27
  3. Replies: 1
    Last Post: 10th February 2009, 09:42
  4. Serializing
    By xyzt in forum Qt Programming
    Replies: 1
    Last Post: 23rd March 2008, 08:51
  5. Can QHash::capacity() be smaller than QHash::size()?!?
    By iw2nhl in forum Qt Programming
    Replies: 2
    Last Post: 24th August 2007, 01:17

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.