Results 1 to 3 of 3

Thread: Error serializing d-pointer object using QDataStream

  1. #1
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Error serializing d-pointer object using QDataStream

    Hi, I'm trying to serialize a pimpl object, but get a compilation error. I prepared a minimal example:

    record_p.h
    Qt Code:
    1. #include "record.h"
    2.  
    3. namespace MyApp {
    4.  
    5. class RecordPrivate : public QObject
    6. {
    7. Q_OBJECT
    8.  
    9. public:
    10. RecordPrivate ( Record* qq, QObject* parent = 0 );
    11.  
    12. QString name() const;
    13. uint age() const;
    14.  
    15. private:
    16. Record* const q;
    17.  
    18. QString m_name;
    19. uint m_age;
    20. };
    21.  
    22. }
    To copy to clipboard, switch view to plain text mode 

    record.h
    Qt Code:
    1. namespace MyApp {
    2.  
    3. class RecordPrivate;
    4.  
    5. class Record : public QObject
    6. {
    7. Q_OBJECT
    8. Q_PROPERTY( QString name READ name CONSTANT )
    9. Q_PROPERTY( uint age READ age CONSTANT )
    10.  
    11. public:
    12. explicit Record(QObject *parent = 0);
    13. virtual ~Record();
    14.  
    15. QString name() const;
    16. uint age() const;
    17.  
    18. private:
    19. Q_DISABLE_COPY( Record )
    20. RecordPrivate* const d;
    21. friend class RecordPrivate;
    22.  
    23. };
    24.  
    25. typedef QSharedPointer<Record> RecordPtr;
    26.  
    27. }
    28.  
    29. QDataStream& operator<<( QDataStream& dataStream, const MyApp::RecordPtr record );
    30.  
    31. Q_DECLARE_METATYPE( MyApp::RecordPtr )
    To copy to clipboard, switch view to plain text mode 

    record.cpp
    Qt Code:
    1. #include "record_p.h"
    2.  
    3. using namespace MyApp;
    4.  
    5. RecordPrivate::RecordPrivate ( Record* qq, QObject* parent ) :
    6. QObject ( parent ),
    7. q ( qq )
    8. {
    9. }
    10.  
    11. uint RecordPrivate::age() const
    12. {
    13. return m_age;
    14. }
    15.  
    16. QString RecordPrivate::name() const
    17. {
    18. return m_name;
    19. }
    20.  
    21. Record::Record(QObject *parent) :
    22. QObject(parent),
    23. d ( new RecordPrivate ( this ) )
    24. {
    25. }
    26.  
    27. Record::~Record()
    28. {
    29. delete d;
    30. }
    31.  
    32. uint Record::age() const
    33. {
    34. return d->age();
    35. }
    36.  
    37. QString Record::name() const
    38. {
    39. return d->name();
    40. }
    41.  
    42. QDataStream& operator<<( QDataStream& dataStream, const RecordPtr record )
    43. {
    44. for(int i=0; i< record->metaObject()->propertyCount(); ++i) {
    45. if(record->metaObject()->property(i).isStored(record)) {
    46. dataStream << record->metaObject()->property(i).read(record);
    47. }
    48. }
    49. return dataStream;
    50. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Giuseppe CalÃ

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Error serializing d-pointer object using QDataStream

    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,
    _

  3. #3
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Error serializing d-pointer object using QDataStream

    Quote Originally Posted by anda_skoa View Post
    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:

    Qt Code:
    1. QDataStream& operator<<( QDataStream& dataStream, const RecordPtr record )
    2. {
    3. for(int i=0; i< record->metaObject()->propertyCount(); ++i) {
    4. if(record->metaObject()->property(i).isStored(record.data())) {
    5. dataStream << record->metaObject()->property(i).read(record.data());
    6. }
    7. }
    8. return dataStream;
    9. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jiveaxe; 12th December 2013 at 15:24.
    Giuseppe CalÃ

Similar Threads

  1. Object serialization with QDataStream
    By jahsiotr in forum Newbie
    Replies: 1
    Last Post: 20th January 2013, 22:04
  2. QDataStream and QSql Object. Urgent!! please
    By ruben.rodrigues in forum Qt Programming
    Replies: 2
    Last Post: 15th August 2011, 23:57
  3. Help with Q_PROPERTY with object pointer
    By lni in forum Qt Programming
    Replies: 1
    Last Post: 16th January 2009, 17:31
  4. Replies: 8
    Last Post: 27th August 2007, 15:45
  5. QDataStream, QTreeWidgetItem, pointer
    By baca in forum Qt Programming
    Replies: 6
    Last Post: 16th February 2007, 16:59

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.