Results 1 to 5 of 5

Thread: Serialization

  1. #1
    Join Date
    May 2006
    Posts
    19
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Serialization

    Hello All,

    Is it possible to serialize instances of my own classes so I can store them on disk? These classes contain STL types such as string & vector<string>. Would I be better off using QString and QStringList types if I was to serialize them?

    Cheers,
    Donal

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Serialization

    Sounds like a job for QDataStream. Pay attention to the Detailed Description.
    J-P Nurmi

  3. #3
    Join Date
    May 2006
    Posts
    19
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Serialization

    Hi,
    Cheers for the reply.

    I've had a look at QDataStream.
    But that ony seems to support native types (int, char, etc)

    How can u use it to store something like a QString or an instance of my own class?

    Cheers

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Serialization

    I told you to pay attention to the detailed description..
    Each item written to the stream is written in a predefined binary format that varies depending on the item's type. Supported Qt types include QBrush, QColor, QDateTime, QFont, QPixmap, QString, QVariant and many others. For the complete list of all Qt types supporting data streaming see the Format of the QDataStream operators.
    You can write QDataStream operators for your class:
    Qt Code:
    1. QDataStream& operator << (QDataStream& stream, const SomeClass& some)
    2. {
    3. stream << ...
    4. return stream;
    5. }
    6.  
    7. QDataStream& operator >> (QDataStream& stream, SomeClass& some)
    8. {
    9. ...
    10. stream >> ..
    11. return stream;
    12. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. The following 2 users say thank you to jpn for this useful post:

    Carlton (21st February 2009), donmorr (17th November 2006)

  6. #5
    Join Date
    Feb 2006
    Posts
    26
    Thanked 2 Times in 1 Post
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: Serialization

    Overloaded operators << and >> for other data types are defined in the data class itself, not in the QDataStream class. This is better for the extensibility of the QDataStream class (in other words, you don't need to modify the QDataStream class itself to extend its supported types).

    That means you have to look in each class you want to serialize if the functions like that exist :

    QDataStream & operator<< ( QDataStream & stream, const QString & string )
    QDataStream & operator>> ( QDataStream & stream, QString & string )

    EDIT: In addition, this url shows you all data serializable with QDataStream, given the Qt framework.

    For you own needs, you will probably define you own overloaded operators for you specific data class.

  7. The following 2 users say thank you to nouknouk for this useful post:

    donmorr (17th November 2006), gfunk (16th November 2006)

Similar Threads

  1. [Qt4] qdatastream/qstring serialization
    By fabo in forum Qt Programming
    Replies: 4
    Last Post: 19th April 2006, 18:31

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.