Results 1 to 6 of 6

Thread: Problem with QDataStream operators

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Problem with QDataStream operators

    I have a class derived from QMap<> which I am using to store an association between two string values. I am having trouble with the serialization using QDataStream operator<<() and operator>>(). Here is the complete code for the class:

    Qt Code:
    1. // Header .h
    2.  
    3. #include <QMetaType>
    4. #include <QMap>
    5. #include <QString>
    6. #include <QVariant>
    7.  
    8.  
    9. class CClassColorMap
    10. : public QMap< QString, QVariant >
    11. {
    12. public:
    13. CClassColorMap();
    14. CClassColorMap( const CClassColorMap & rhs );
    15. virtual ~CClassColorMap();
    16. };
    17.  
    18. Q_DECLARE_METATYPE( CClassColorMap );
    19.  
    20. QDataStream & operator<<( QDataStream & stream, const CClassColorMap & classColors );
    21. QDataStream & operator>>( QDataStream & stream, CClassColorMap & classColors );
    22.  
    23.  
    24. // Implementation .cpp
    25.  
    26. #include "ClassColorMap.h"
    27. #include <QDataStream>
    28.  
    29. static CClassColorMap sClassColors;
    30. static bool sbRegistered = false;
    31.  
    32. CClassColorMap::CClassColorMap(void)
    33. : QMap< QString, QVariant >()
    34. {
    35. if ( !sbRegistered )
    36. {
    37. qRegisterMetaTypeStreamOperators< CClassColorMap >( "CClassColorMap" );
    38. sbRegistered = true;
    39. }
    40. }
    41.  
    42. CClassColorMap::CClassColorMap( const CClassColorMap & rhs )
    43. : QMap< QString, QVariant >()
    44. {
    45. if ( !sbRegistered )
    46. {
    47. qRegisterMetaTypeStreamOperators< CClassColorMap >( "CClassColorMap" );
    48. sbRegistered = true;
    49. }
    50.  
    51. *this = rhs;
    52. }
    53.  
    54. CClassColorMap::~CClassColorMap(void)
    55. {
    56. }
    57.  
    58. QDataStream & operator<<( QDataStream & stream, const CClassColorMap & classColors )
    59. {
    60. return operator<<( stream, (const QMap< QString, QVariant > &) classColors );
    61. }
    62.  
    63. QDataStream & operator>>( QDataStream & stream, CClassColorMap & classColors )
    64. {
    65. return operator>>( stream, (QMap< QString, QVariant > &) classColors );
    66. }
    To copy to clipboard, switch view to plain text mode 

    I am using this for QSettings. The issue is that the QDataStream operators are never called, either when retrieving the QSettings instance or when storing it. I verified this by settings breakpoints in the dbugger.

    I have other standalone custom classes (i.e. not derived from Qt base classes) that are used in the same QSettings, and that are declared and implemented in the identical fashion as the class above. The QDataStream operators are invoked just fine in those cases (again using the debugger to verify).

    Can anyone see what is wrong with the definition above? Or suggest a reason why this isn't working? The only critical difference is the QMap<> base class, but I don't understand why this could cause a problem.

    Update: I changed the class so it uses the QMap<> as a member variable rather than a base class. Now the stream operators are invoked. Why?
    Last edited by d_stranz; 3rd September 2014 at 00:47.

Similar Threads

  1. Regular Expression for Operators
    By parulkalra14 in forum Qt Programming
    Replies: 6
    Last Post: 7th January 2014, 10:36
  2. Define a QRegExp containing arithmetic operators
    By aaditya190 in forum Newbie
    Replies: 1
    Last Post: 4th December 2013, 11:38
  3. Store QVector to QDataStream problem
    By ruben.rodrigues in forum Newbie
    Replies: 1
    Last Post: 2nd August 2010, 09:27
  4. QDataStream class/struct & stream operators
    By darksaga in forum Qt Programming
    Replies: 1
    Last Post: 1st August 2008, 19:40
  5. Problem with QTcpSocket and QDataStream
    By Valheru in forum Qt Programming
    Replies: 4
    Last Post: 16th September 2006, 13:08

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.