Results 1 to 2 of 2

Thread: Serialization arbitary data types

  1. #1
    Join Date
    Mar 2011
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Serialization arbitary data types

    Hi
    I wrote a program ( that will come below ) that has 2 class with names : keyNode and valueNode and every class has 2 private member . now I create QMap<keyNode , valueNode > and I want to serialize this QMap with QDataStream and I know that I must override operator<<() and operator>>() but I dont know how . please help me .
    thanks

    Qt Code:
    1. #include <QMap>
    2. #include <QString>
    3. #include <conio.h>
    4. #include <QFile>
    5. #include <QDataStream>
    6. #include <QIODevice>
    7. #include <QtCore/QCoreApplication>
    8.  
    9.  
    10. class valueNode
    11. {
    12. public:
    13. valueNode()
    14. {
    15. docId = 0 ;
    16. termFreq = 0 ;
    17. }
    18. valueNode( qint32 doc , qint32 term )
    19. {
    20. docId = doc ;
    21. termFreq = term ;
    22. }
    23. void setDocId( qint32 newDocId )
    24. {
    25. docId = newDocId ;
    26. }
    27. void setTermFreq( qint32 newTermFreq )
    28. {
    29. termFreq = newTermFreq ;
    30. }
    31. qint32 getDocId() const
    32. {
    33. return docId ;
    34. }
    35. qint32 getTermFreq() const
    36. {
    37. return termFreq ;
    38. }
    39.  
    40. private:
    41. qint32 docId ;
    42. qint32 termFreq ;
    43. };
    44.  
    45.  
    46. class keyNode
    47. {
    48. public:
    49. keyNode()
    50. {
    51.  
    52. docFreq = 0 ;
    53. }
    54. keyNode( QString k , qint32 doc )
    55. {
    56. key = k ;
    57. docFreq = doc ;
    58. }
    59. void setKey( QString newKey )
    60. {
    61. key = newKey ;
    62. }
    63. void setDFreocq( qint32 newDocFreq )
    64. {
    65. docFreq = newDocFreq ;
    66. }
    67. QString getKey() const
    68. {
    69. return key ;
    70. }
    71. qint32 getDocFreq() const
    72. {
    73. return docFreq ;
    74. }
    75.  
    76. private:
    77. QString key ;
    78. qint32 docFreq ;
    79. };
    80.  
    81.  
    82.  
    83. inline bool operator<(const keyNode &e1, const keyNode &e2)
    84. {
    85. return e1.getKey() < e2.getKey() ;
    86. }
    87.  
    88.  
    89.  
    90. int main(int argc, char *argv[])
    91. {
    92. QCoreApplication a(argc, argv);
    93.  
    94. QMap< keyNode , valueNode > map ;
    95. QMap< QString , valueNode > mmap ;
    96.  
    97. map[ keyNode( "school" , 18 ) ] = valueNode( 12 , 17 ) ;
    98. map[ keyNode( "class" , 45 ) ] = valueNode( 122 , 76 ) ;
    99. map[ keyNode( "student" , 23 ) ] = valueNode( 1 , 5 ) ;
    100. map[ keyNode( "amir" , 32 ) ] = valueNode( 187 , 58 ) ;
    101. map[ keyNode( "xoom" , 179 ) ] = valueNode( 139 , 25 ) ;
    102. map[ keyNode( "class" , 1115 ) ] = valueNode( 1002 , 876 ) ;
    103.  
    104. mmap[ "v" ] = valueNode( 11 , 12 ) ;
    105. mmap[ "a" ] = valueNode( 112 , 3022 ) ;
    106. mmap[ "c" ] = valueNode( 91 , 762 ) ;
    107. mmap[ "x" ] = valueNode( 654 , 102 ) ;
    108.  
    109. QFile file("file.dat");
    110. file.open(QIODevice::WriteOnly);
    111. QDataStream out(&file); // we will serialize the data into the file
    112. out << map ; // serialize a string
    113. file.close() ;
    114. map.clear() ;
    115.  
    116.  
    117. file.open(QIODevice::ReadOnly);
    118. QDataStream in(&file); // read the data serialized from the file
    119. in >> map;
    120. file.close() ;
    121. getch() ;
    122. return a.exec();
    123. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by amiref; 19th March 2011 at 17:24.

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Serialization arbitary data types

    Hi!

    You will have to implement these functions (see http://doc.qt.nokia.com/4.7/qdatastr...her-qt-classes)

    Qt Code:
    1. QDataStream & operator<< (QDataStream& stream, const keyNode& key)
    2. {
    3. // break down the composite class to primitives and stream it..
    4. stream << key.getKey() << key.getDocFreq();
    5. return stream;
    6. }
    7. QDataStream & operator>> (QDataStream& stream, keyNode& key)
    8. {
    9. QString keyS;qint32 docFreq;
    10. stream >> keyS >> docFreq;
    11. key.setKey(keyS);
    12. key.setDFreocq(docFreq);
    13. return stream;
    14. }
    15. QDataStream & operator<< (QDataStream& stream, const valueNode& value);
    16. QDataStream & operator>> (QDataStream& stream, valueNode& value);
    To copy to clipboard, switch view to plain text mode 
    You could also use the private fields instead of the accessor-functions but then you need to declare the operators as friends to the class.

    HIH

    Joh
    Last edited by JohannesMunk; 20th March 2011 at 10:10.

Similar Threads

  1. Different data types in a QSqlQueryModel
    By Lodas in forum Qt Programming
    Replies: 1
    Last Post: 27th February 2008, 16:31
  2. conversion of binay data to qt types
    By rajeshs in forum Qt Programming
    Replies: 16
    Last Post: 4th January 2008, 12:26
  3. serialization & data integrity
    By darksaga in forum Qt Programming
    Replies: 2
    Last Post: 28th August 2007, 13:04
  4. system-independent C++ data types
    By magland in forum General Programming
    Replies: 15
    Last Post: 28th March 2007, 20:33
  5. Use QVariant with custon data types
    By mcosta in forum Qt Programming
    Replies: 4
    Last Post: 11th January 2006, 14:55

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
  •  
Qt is a trademark of The Qt Company.