Results 1 to 5 of 5

Thread: serialising reading writing

  1. #1
    Join Date
    Jan 2006
    Location
    Munich, Germany.
    Posts
    111
    Thanks
    29
    Thanked 3 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default serialising reading writing

    Sorry if this is a silly question:

    I've got a (very simple) class with lots of member variables. It's more like a 'struct' with a clear() function.
    I want to get all the member variables written into a file (and later have the file read in again). I thought I might define operators '<<' and '>>' for the class.
    But I still don't quite see it. That won't help my QDataStream, will it?

    Instead, should I write a 'serialise(QDataStream& out)' and a 'QDataStream serialise()'
    or 'QByteArray serialise()' and 'serialise(QByteArray)' and then write/read that to/from the file?

    thanks
    K

  2. #2
    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: serialising reading writing

    I am not sure I understand what you want.
    But if you just want to serialize data in to a file then you don't need much, Qt has this out of the box for regular types (int,float,etc.. and QTypes) :
    Qt Code:
    1. QFile *file = new QFile("file.dat");
    2. QDataStream os(file);
    3. if(file->open(QIODevice::Append)
    4. {
    5. os<<myClass->m_someRegularTypeVar;
    6. }
    To copy to clipboard, switch view to plain text mode 

    You can overlard the global QDataStream::operator<<() and >>() if you need to serialize custom types.

  3. The following user says thank you to high_flyer for this useful post:

    TheKedge (5th April 2007)

  4. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany.
    Posts
    111
    Thanks
    29
    Thanked 3 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: serialising reading writing

    Yep, thanks, that has answered it.

    The question was basically even simpler.

    I have 50 member variables, and don't want to use << in big lists when I need to write save the class interna. So I could:
    a) overload the QDataStream
    or
    b) have some member function taking a QDataStream&

    I think I'll choose (b)
    thanks again

    K

  5. #4
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: serialising reading writing

    Quote Originally Posted by TheKedge View Post
    a) overload the QDataStream
    Just create a new handler :
    Qt Code:
    1. QDataStream& operator << (QDataStream& out, MyStruct *s)
    2. {
    3. // serialization code goes here
    4. }
    5.  
    6. QDataStream& operator >> (QDataStream& in, MyStruct *s)
    7. {
    8. // deserialization code goes here
    9. }
    To copy to clipboard, switch view to plain text mode 

    Current Qt projects : QCodeEdit, RotiDeCode

  6. The following user says thank you to fullmetalcoder for this useful post:

    TheKedge (5th April 2007)

  7. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany.
    Posts
    111
    Thanks
    29
    Thanked 3 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: serialising reading writing

    Ahhhh!
    brilliant, understood.
    thanks
    K

Similar Threads

  1. Qt Designer or writing by hand
    By luf in forum Qt Tools
    Replies: 3
    Last Post: 12th March 2007, 12:31
  2. reading and writing data from a QTableWidget
    By zorro68 in forum Qt Programming
    Replies: 4
    Last Post: 29th January 2007, 20:51
  3. Replies: 6
    Last Post: 8th January 2007, 10:24
  4. Replies: 6
    Last Post: 27th February 2006, 12:47

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.