Results 1 to 14 of 14

Thread: how to use >> operator serialize QValueList<T> to QDataStream?

  1. #1
    Join Date
    Jan 2006
    Posts
    55
    Thanks
    13
    Qt products
    Qt3
    Platforms
    Unix/X11

    Unhappy how to use >> operator serialize QValueList<T> to QDataStream?

    Below is the related instruction in qt.


    QDataStream & operator<< ( QDataStream & s, const QValueList<T> & l )
    This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
    Writes a list, l, to the stream s. The type T stored in the list must implement the streaming operator.
    QDataStream & operator>> ( QDataStream & s, QValueList<T> & l )
    Reads a list, l, from the stream s. The type T stored in the list must implement the streaming operator.



    I have realize >> operator in my "T", as below:

    Qt Code:
    1. class CSysAlarm
    2. {
    3.  
    4. public:
    5. CSysAlarm(ENUM_INFO_TYPE infoType=COS_INFO);
    6. ~CSysAlarm();
    7. QDataStream& operator <<( QDataStream& stream );
    8. // Attributes
    9. public:
    10.  
    11. ENUM_INFO_TYPE m_enumInfoType;
    12. bool m_bIfAlarm;
    13. uint m_dwAlarmTimes;
    14. bool m_bAutoPrint;
    15. bool m_bIfPop;
    16. bool m_bIfLoop;
    17. bool m_bAllInfo;
    18. QString m_strPathFile;
    19.  
    20. public:
    21.  
    22. };
    To copy to clipboard, switch view to plain text mode 

    And other code:

    Qt Code:
    1. QFile file( "hmiset.bin" );
    2. if(file.open(IO_WriteOnly )==false)
    3. return;
    4. QDataStream stream( &file );
    To copy to clipboard, switch view to plain text mode 

    How can I do this as title? thanks!
    Last edited by wysota; 18th May 2006 at 10:27.

  2. #2
    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: how to use >> operator serialize QValueList<T> to QDataStream?

    But what is wrong? Do you get any messages from the compiler or anything?

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

    coralbird (17th May 2006)

  4. #3
    Join Date
    Jan 2006
    Posts
    55
    Thanks
    13
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: how to use >> operator serialize QValueList<T> to QDataStream?

    Quote Originally Posted by wysota
    But what is wrong? Do you get any messages from the compiler or anything?

    The problem is that i don't know how to realize the function as title.The Key is
    that how to use << operator as below:

    QDataStream & operator<< ( QDataStream & s, const QValueList<T> & l )

    For example:
    typedef QValueList<CSysAlarm> CSysAlarmList;

    CSysAlarmList m_AlarmCfgList;

    <<(stream,m_AlarmCfgList) ?
    Last edited by coralbird; 17th May 2006 at 01:44.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to use >> operator serialize QValueList<T> to QDataStream?

    Qt Code:
    1. QDataStream& operator <<( QDataStream& stream, const CSysAlarm& alarm )
    2. {
    3. ...
    4. return stream;
    5. }
    6.  
    7. QDataStream& operator >>( QDataStream& stream, CSysAlarm& alarm )
    8. {
    9. ...
    10. return stream;
    11. }
    12.  
    13. ...
    14.  
    15. stream << m_AlarmCfgList;
    To copy to clipboard, switch view to plain text mode 

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

    coralbird (17th May 2006)

  7. #5
    Join Date
    Jan 2006
    Posts
    55
    Thanks
    13
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: how to use >> operator serialize QValueList<T> to QDataStream?

    Quote Originally Posted by jacek
    Qt Code:
    1. QDataStream& operator <<( QDataStream& stream, const CSysAlarm& alarm )
    2. {
    3. ...
    4. return stream;
    5. }
    6.  
    7. QDataStream& operator >>( QDataStream& stream, CSysAlarm& alarm )
    8. {
    9. ...
    10. return stream;
    11. }
    12.  
    13. ...
    14.  
    15. stream << m_AlarmCfgList;
    To copy to clipboard, switch view to plain text mode 
    It can work ! Thank wysota and jacek.
    does only QValueList supprot serializing?If I have several classes inherited from CSysAlarm,and I have to abandon QValueList ,In which way i can serialize them expendiently like using QValueList?
    Last edited by coralbird; 17th May 2006 at 03:57.

  8. #6
    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: how to use >> operator serialize QValueList<T> to QDataStream?

    All Qt classes which have the operator>>(QDataStream&, ...) and operator<<(QDataStream&, ...) defined support serialisation out of the box. Take a look at the docs.

    http://doc.trolltech.com/3.3/datastreamformat.html

  9. #7
    Join Date
    Jan 2006
    Posts
    55
    Thanks
    13
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: how to use >> operator serialize QValueList<T> to QDataStream?

    Quote Originally Posted by wysota
    All Qt classes which have the operator>>(QDataStream&, ...) and operator<<(QDataStream&, ...) defined support serialisation out of the box. Take a look at the docs.

    http://doc.trolltech.com/3.3/datastreamformat.html
    I understand what the docs mean.I want to find a simple way to solve
    the probleb as below:

    Qt Code:
    1. class a
    2. {
    3. ...
    4. };
    5. class b:public a
    6. {
    7. ...
    8. }
    9. class c:public a
    10. {
    11. ....
    12. }
    13. .....
    To copy to clipboard, switch view to plain text mode 
    I can't use QValueList to store a ,b and c ,for QvalueList<a> store class object itself in it,not pointer.Maybe I have to use QPtrList<a> to store them.
    In Mfc,using virtual function it is very easy to realize this.

    Thanks!
    Last edited by wysota; 18th May 2006 at 10:26.

  10. #8
    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: how to use >> operator serialize QValueList<T> to QDataStream?

    But what is the problem? You don't have to use a class which has streaming operators implemented to store your data.

    Qt Code:
    1. QPtrList<MyClass> list;
    2. QDataStream ds(...);
    3. //...
    4. ds << list.size();
    5. for(QPtrList<MyClass>::iterator it = list.begin(); it!=list.end(); ++it){
    6. ds << *(*it);
    7. }
    To copy to clipboard, switch view to plain text mode 

  11. #9
    Join Date
    Jan 2006
    Posts
    55
    Thanks
    13
    Qt products
    Qt3
    Platforms
    Unix/X11

    Smile Re: how to use >> operator serialize QValueList<T> to QDataStream?

    Quote Originally Posted by wysota
    But what is the problem? You don't have to use a class which has streaming operators implemented to store your data.

    Qt Code:
    1. QPtrList<MyClass> list;
    2. QDataStream ds(...);
    3. //...
    4. ds << list.size();
    5. for(QPtrList<MyClass>::iterator it = list.begin(); it!=list.end(); ++it){
    6. ds << *(*it);
    7. }
    To copy to clipboard, switch view to plain text mode 

    Thanks , I will test soon .
    I think qt is using a differant mechanism to serialize than MFC.
    and there is still a problem,after storing data like above,how to restore data from QDataStream?
    Qt Code:
    1. QPtrList<MyClass> list;
    2. QDataStream ds(...)
    3. //...
    4. int nSize;
    5. ds>>nSize;
    6. for(int i=0;i<nSize;i++)
    7. {
    8. //how to write the code?
    9. }
    To copy to clipboard, switch view to plain text mode 
    Thanks!
    Last edited by wysota; 18th May 2006 at 10:26.

  12. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to use >> operator serialize QValueList<T> to QDataStream?

    Quote Originally Posted by coralbird
    how to restore data from QDataStream?
    Analogically:
    Qt Code:
    1. for(int i=0;i<nSize;i++)
    2. {
    3. MyClass *ptr = new MyClass();
    4. ds >> *ptr;
    5. list.append( ptr );
    6. }
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. for(int i=0;i<nSize;i++)
    2. {
    3. MyClass *ptr = MyClass::load( ds );
    4. if( ptr != 0 ) {
    5. list.append( ptr );
    6. }
    7. else {
    8. // error
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

  13. #11
    Join Date
    Jan 2006
    Posts
    55
    Thanks
    13
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: how to use >> operator serialize QValueList<T> to QDataStream?

    This is my test code and result as below:
    Qt Code:
    1. class Ca
    2. {
    3. public:
    4. int x;
    5. };
    6. class Cb: public Ca
    7. {
    8. public:
    9. int y;
    10. };
    11. //note:this function is called two times.
    12. QDataStream& operator<<( QDataStream& stream ,const Ca& a1)
    13. {
    14. stream<<a1.x;
    15. return stream;
    16. }
    17. //note:this function is not called.
    18. QDataStream& operator<<( QDataStream& stream ,const Cb& b1)
    19. {
    20. stream<<b1.x;
    21. stream<<b1.y;
    22. return stream;
    23. }
    24. int myTestfunc()
    25. {
    26. QPtrList<Ca> list;
    27. QFile file;
    28. file.setName("test.bin");
    29. if(!file.open(IO_WriteOnly))
    30. return 1;
    31. Ca* pCa=new Ca;
    32. Cb* pCb=new Cb;
    33. list.append(pCa);
    34. list.append(pCb);
    35. QDataStream ds(&file);
    36. ds<<list.count();
    37. for(QPtrList<Ca>::iterator it=list.begin();it!=list.end();++it)
    38. {
    39. ds<<*(*it);
    40. }
    41. return 1;
    42. }
    To copy to clipboard, switch view to plain text mode 
    Please give me idea where to modify my code,thanks!
    Last edited by wysota; 18th May 2006 at 10:25.

  14. #12
    Join Date
    Jan 2006
    Posts
    55
    Thanks
    13
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: how to use >> operator serialize QValueList<T> to QDataStream?

    Quote Originally Posted by jacek
    Analogically:
    Qt Code:
    1. for(int i=0;i<nSize;i++)
    2. {
    3. MyClass *ptr = new MyClass();
    4. ds >> *ptr;
    5. list.append( ptr );
    6. }
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. for(int i=0;i<nSize;i++)
    2. {
    3. MyClass *ptr = MyClass::load( ds );
    4. if( ptr != 0 ) {
    5. list.append( ptr );
    6. }
    7. else {
    8. // error
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 
    may it is same as qvaluelist.h realized:
    Qt Code:
    1. template <class T>
    2. Q_INLINE_TEMPLATES QDataStream& operator>>( QDataStream& s, QValueList<T>& l )
    3. {
    4. l.clear();
    5. Q_UINT32 c;
    6. s >> c;
    7. for( Q_UINT32 i = 0; i < c; ++i )
    8. {
    9. T t;
    10. s >> t;
    11. l.append( t );
    12. if ( s.atEnd() )
    13. break;
    14. }
    15. return s;
    16. }
    To copy to clipboard, switch view to plain text mode 

    Now I want to realize several kind clssses serializing.Should I have to record every class's
    kind myself,and calss's data ,then according to the kind to restore the data?In fact ,MFC recorded these information.
    Qt Code:
    1. for(int i=0;i<nSize;i++)
    2. {
    3. int nKind;
    4. ds>>nKind;
    5. if(nKind==Ca::rtti())
    6. {
    7. ca*ptr = new ca();
    8. ds >> *ptr;
    9. list.append( ptr );
    10. }
    11. else if(nKind==Cb::rtti())
    12. {
    13. cb*ptr = new cb();
    14. ds >> *ptr;
    15. list.append( ptr );
    16. }
    17. ...
    18.  
    19.  
    20. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 18th May 2006 at 10:25.

  15. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to use >> operator serialize QValueList<T> to QDataStream?

    Quote Originally Posted by coralbird
    Now I want to realize several kind clssses serializing.Should I have to record every class's kind myself,and calss's data ,then according to the kind to restore the data?In fact ,MFC recorded these information.
    You should be able to read your objects using QVariant, if you use these:
    int qRegisterMetaType ( const char * typeName )
    Registers the type name typeName to the type T. Returns the internal ID used by QMetaType. Any class or struct that has a public constructor, a public copy constructor, and a public destructor can be registered.
    After a type has been registered, you can create and destroy objects of that type dynamically at run-time.
    This example registers the class MyClass:
    qRegisterMetaType<MyClass>("MyClass");
    Note: This function is thread-safe.
    See also qRegisterMetaTypeStreamOperators(), QMetaType::isRegistered(), and Q_DECLARE_METATYPE().

    void qRegisterMetaTypeStreamOperators ( const char * typeName )
    Registers the stream operators for the type T called typeName.
    Afterward, the type can be streamed using QMetaType::load() and QMetaType::save(). These functions are used when streaming a QVariant.
    qRegisterMetaTypeStreamOperator<MyClass>("MyClass" );
    The stream operators should have the following signatures:
    QDataStream &operator<<(QDataStream &out, const MyClass &myObj);
    QDataStream &operator>>(QDataStream &in, MyClass &myObj);
    Note: This function is thread-safe.
    See also qRegisterMetaType(), QMetaType::isRegistered(), and Q_DECLARE_METATYPE().

  16. The following user says thank you to jacek for this useful post:

    coralbird (19th May 2006)

  17. #14
    Join Date
    Jan 2006
    Posts
    55
    Thanks
    13
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: how to use >> operator serialize QValueList<T> to QDataStream?

    Thank wysoto and jacek for giving me so much help. I will study them soon.

Similar Threads

  1. QDataStream >> QString
    By smalls in forum Qt Programming
    Replies: 2
    Last Post: 17th January 2006, 22:14

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.