Results 1 to 6 of 6

Thread: Serializing nested objects

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

    Default Serializing nested objects

    Hi, I need your help :P Scenario:

    * I created objectA containing a bunch of Q_PROPERTY() and stream operators to serialize it. The code is ok, objectA's properties are saved correctly
    * I created objectB containing a bunch of Q_PROPERTY() and objectA is in one of them. When I try to serialize objectB I got the following message in console:

    QVariant::save: unable to save type 322.
    The resulting file contains all objectB properties except objectA (I can see only the object name, something like MyApp::ObjectA).

    Any idea where I'm wrong?

    Very thanks.
    Giuseppe CalÃ

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Serializing nested objects

    Any idea where I'm wrong?
    Based on the information you have provided you are doing something wrong with a QVariant.

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

    Default Re: Serializing nested objects

    Well, I don't think this is the problem. Trying to be more clear:

    When I use stream operator on an indipendent objectA (not as data member of objectB) I got a correct result, this should tell me that the stream operator in objecta.h/cpp is ok, right? objectB has its own stream operator, and has objectA as data member. Calling the stream operator of objectB gives the error in first post. If in objectB I comment out the Q_PROPERTY() line regarding objectA (so it is not saved during stream operation) then it works. Probably I'm doing a wrong assumption: I presume that calling operator<< of objectB triggers operator<< of objectA when this one must be saved; am I wrong? (I know, a bit intricate). A very limited example:

    objecta.h
    Qt Code:
    1. class ObjectA : public QObject
    2. {
    3. Q_OBJECT
    4. Q_PROPERTY( QString name READ name WRITE setName )
    5. ...
    6. };
    7.  
    8. QDataStream& operator<<( QDataStream& dataStream, const ObjectA & objectA );
    To copy to clipboard, switch view to plain text mode 

    objectb.h
    Qt Code:
    1. class ObjectB : public QObject
    2. {
    3. Q_OBJECT
    4. Q_PROPERTY( ObjectA objectA READ objectA WRITE setObjectA )
    5. Q_PROPERTY( QString path READ path WRITE setPath )
    6. ...
    7. };
    8.  
    9. QDataStream& operator<<( QDataStream& dataStream, const ObjectB & objectB );
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. ObjectA objectA;
    2. out << objectA; // works
    3.  
    4. ObjectB objectB;
    5. objectB.setObjectA(objectA)
    6. out << objectB; // NOT works: QVariant::save: unable to save type 322.
    To copy to clipboard, switch view to plain text mode 
    Last edited by jiveaxe; 12th December 2013 at 23:21.
    Giuseppe CalÃ

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Serializing nested objects

    You have implemented operator<<() for class ObjectB. We cannot see this code.

    I can only assume from the involvement of QVariant in the error and the implied connection between a Q_PROPERTY macro and serialisation that you are accessing the property values through QMetaObject and QMetaProperty::read(). Have you declared ObjectA* and its stream operators qRegisterMetaType<T>() and qRegisterMetaTypeStreamOperators<T>()? (You cannot declare for ObjectA because it lacks the requisite copy constructor)

  5. The following user says thank you to ChrisW67 for this useful post:


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

    Default Re: Serializing nested objects

    Sorry, here the missing code:

    Qt Code:
    1. QDataStream& operator<<( QDataStream& dataStream, const ObjectA & objectA )
    2. {
    3. for(int i=0; i< objectA.metaObject()->propertyCount(); ++i) {
    4. if(objectA.metaObject()->property(i).isStored(&objectA)) {
    5. dataStream << objectA.metaObject()->property(i).read(&objectA);
    6. }
    7. }
    8. return dataStream;
    9. }
    To copy to clipboard, switch view to plain text mode 

    (stream operator for ObjectB is the same, just replaced ObjectA with ObjectB)

    Quote Originally Posted by ChrisW67 View Post
    Have you declared ObjectA* and its stream operators qRegisterMetaType<T>() and qRegisterMetaTypeStreamOperators<T>()?
    Nope. Where those line should be added?

    I tried adding:

    Qt Code:
    1. qRegisterMetaType<ObjectA>("ObjectA");
    2. qRegisterMetaTypeStreamOperators<ObjectA>("ObjectA");
    To copy to clipboard, switch view to plain text mode 

    in objecta.h but make returns the following error:
    objecta.h:28: error: specializing member '::qRegisterMetaType<ObjectA>' requires 'template<>' syntax
    qRegisterMetaType<ObjectA>("ObjectA");
    Thanks for your help.
    Giuseppe CalÃ

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

    Default Re: Serializing nested objects

    Ok, solved; I added:

    Qt Code:
    1. qRegisterMetaType<ObjectA>("ObjectA");
    2. qRegisterMetaTypeStreamOperators<ObjectA>("ObjectA");
    To copy to clipboard, switch view to plain text mode 

    in constructor of ObjectB and now serialization with QDataStream works fine.
    Giuseppe CalÃ

Similar Threads

  1. Replies: 2
    Last Post: 12th December 2013, 14:25
  2. Serializing a Qobject
    By erfan in forum Qt Programming
    Replies: 1
    Last Post: 12th December 2012, 17:00
  3. Serializing QHash to a QByteArray
    By PeterThePuter in forum Qt Programming
    Replies: 3
    Last Post: 17th December 2010, 23:19
  4. Serializing QImage
    By lukass in forum Newbie
    Replies: 1
    Last Post: 26th October 2010, 18:58
  5. Serializing
    By xyzt in forum Qt Programming
    Replies: 1
    Last Post: 23rd March 2008, 08:51

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.