d_stranz
3rd September 2014, 00:59
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:
// Header .h
#include <QMetaType>
#include <QMap>
#include <QString>
#include <QVariant>
class QDataStream;
class CClassColorMap
: public QMap< QString, QVariant >
{
public:
CClassColorMap();
CClassColorMap( const CClassColorMap & rhs );
virtual ~CClassColorMap();
};
Q_DECLARE_METATYPE( CClassColorMap );
QDataStream & operator<<( QDataStream & stream, const CClassColorMap & classColors );
QDataStream & operator>>( QDataStream & stream, CClassColorMap & classColors );
// Implementation .cpp
#include "ClassColorMap.h"
#include <QDataStream>
static CClassColorMap sClassColors;
static bool sbRegistered = false;
CClassColorMap::CClassColorMap(void)
: QMap< QString, QVariant >()
{
if ( !sbRegistered )
{
qRegisterMetaTypeStreamOperators< CClassColorMap >( "CClassColorMap" );
sbRegistered = true;
}
}
CClassColorMap::CClassColorMap( const CClassColorMap & rhs )
: QMap< QString, QVariant >()
{
if ( !sbRegistered )
{
qRegisterMetaTypeStreamOperators< CClassColorMap >( "CClassColorMap" );
sbRegistered = true;
}
*this = rhs;
}
CClassColorMap::~CClassColorMap(void)
{
}
QDataStream & operator<<( QDataStream & stream, const CClassColorMap & classColors )
{
return operator<<( stream, (const QMap< QString, QVariant > &) classColors );
}
QDataStream & operator>>( QDataStream & stream, CClassColorMap & classColors )
{
return operator>>( stream, (QMap< QString, QVariant > &) classColors );
}
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?
// Header .h
#include <QMetaType>
#include <QMap>
#include <QString>
#include <QVariant>
class QDataStream;
class CClassColorMap
: public QMap< QString, QVariant >
{
public:
CClassColorMap();
CClassColorMap( const CClassColorMap & rhs );
virtual ~CClassColorMap();
};
Q_DECLARE_METATYPE( CClassColorMap );
QDataStream & operator<<( QDataStream & stream, const CClassColorMap & classColors );
QDataStream & operator>>( QDataStream & stream, CClassColorMap & classColors );
// Implementation .cpp
#include "ClassColorMap.h"
#include <QDataStream>
static CClassColorMap sClassColors;
static bool sbRegistered = false;
CClassColorMap::CClassColorMap(void)
: QMap< QString, QVariant >()
{
if ( !sbRegistered )
{
qRegisterMetaTypeStreamOperators< CClassColorMap >( "CClassColorMap" );
sbRegistered = true;
}
}
CClassColorMap::CClassColorMap( const CClassColorMap & rhs )
: QMap< QString, QVariant >()
{
if ( !sbRegistered )
{
qRegisterMetaTypeStreamOperators< CClassColorMap >( "CClassColorMap" );
sbRegistered = true;
}
*this = rhs;
}
CClassColorMap::~CClassColorMap(void)
{
}
QDataStream & operator<<( QDataStream & stream, const CClassColorMap & classColors )
{
return operator<<( stream, (const QMap< QString, QVariant > &) classColors );
}
QDataStream & operator>>( QDataStream & stream, CClassColorMap & classColors )
{
return operator>>( stream, (QMap< QString, QVariant > &) classColors );
}
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?