PDA

View Full Version : Error serializing d-pointer object using QDataStream



jiveaxe
12th December 2013, 12:50
Hi, I'm trying to serialize a pimpl object, but get a compilation error. I prepared a minimal example:

record_p.h

#include "record.h"

namespace MyApp {

class RecordPrivate : public QObject
{
Q_OBJECT

public:
RecordPrivate ( Record* qq, QObject* parent = 0 );

QString name() const;
uint age() const;

private:
Record* const q;

QString m_name;
uint m_age;
};

}

record.h

namespace MyApp {

class RecordPrivate;

class Record : public QObject
{
Q_OBJECT
Q_PROPERTY( QString name READ name CONSTANT )
Q_PROPERTY( uint age READ age CONSTANT )

public:
explicit Record(QObject *parent = 0);
virtual ~Record();

QString name() const;
uint age() const;

private:
Q_DISABLE_COPY( Record )
RecordPrivate* const d;
friend class RecordPrivate;

};

typedef QSharedPointer<Record> RecordPtr;

}

QDataStream& operator<<( QDataStream& dataStream, const MyApp::RecordPtr record );

Q_DECLARE_METATYPE( MyApp::RecordPtr )

record.cpp


#include "record_p.h"

using namespace MyApp;

RecordPrivate::RecordPrivate ( Record* qq, QObject* parent ) :
QObject ( parent ),
q ( qq )
{
}

uint RecordPrivate::age() const
{
return m_age;
}

QString RecordPrivate::name() const
{
return m_name;
}

Record::Record(QObject *parent) :
QObject(parent),
d ( new RecordPrivate ( this ) )
{
}

Record::~Record()
{
delete d;
}

uint Record::age() const
{
return d->age();
}

QString Record::name() const
{
return d->name();
}

QDataStream& operator<<( QDataStream& dataStream, const RecordPtr record )
{
for(int i=0; i< record->metaObject()->propertyCount(); ++i) {
if(record->metaObject()->property(i).isStored(record)) {
dataStream << record->metaObject()->property(i).read(record);
}
}
return dataStream;
}


record.cpp:-1: In function 'QDataStream& operator<<(QDataStream&, MyApp::RecordPtr)':
no matching function for call to 'QMetaProperty::isStored(const RecordPtr&)'
if(record->metaObject()->property(i).isStored(record)) {
^
candidate is:
bool QMetaProperty::isStored(const QObject*) const
bool isStored(const QObject *obj = 0) const;
^
note: no known conversion for argument 1 from 'const RecordPtr {aka const QSharedPointer<MyApp::Record>}' to 'const QObject*'

Where is my error?

Very thanks.

anda_skoa
12th December 2013, 14:16
The compiler already told you the error, didn't it?


no known conversion for argument 1 from 'const RecordPtr {aka const QSharedPointer<MyApp::Record>}' to 'const QObject*'

you are trying to pass an object of type RecordPtr to a method expecting "const QObject*".

Cheers,
_

jiveaxe
12th December 2013, 15:24
you are trying to pass an object of type RecordPtr to a method expecting "const QObject*".
_

This is obvious but doesn't help me to track my error. If I use a plain Record class (no pimpl, no shared pointer), the compiler is happy despite passing a Record object. So probably I should "cast" QSharedPointer<MyApp::Record> to Record, but don't know how...

Added after 59 minutes:

Ok, solution found; it was easy: QSharedPointer::data ().

This is the correct code:


QDataStream& operator<<( QDataStream& dataStream, const RecordPtr record )
{
for(int i=0; i< record->metaObject()->propertyCount(); ++i) {
if(record->metaObject()->property(i).isStored(record.data())) {
dataStream << record->metaObject()->property(i).read(record.data());
}
}
return dataStream;
}