Serialize QObject to QString



  • Hello, everybody!

    Could you suggest me best way to serialize objects that inherited from QObject with meta information?
    Just one way I have for now is this:
    Serialize objects to QDataStream and save binary data in text. Then parse class meta information manually and append to result string.



  • I know no other way for that, sorry



  • Very bad, I found that qjson lib can serialize meta information and member values, but he did not save signals, slots and other meta attributes.



  • I think you will need to write your own implementation of:

    @
    QTextStream operator << ( QTextStream& stream, QObject* obj )
    @

    that iterates through the properties and signal/slots of the metaobject and then recurses down to the child objects.



  • Yes, it deal tip, thank you!



  • [quote author="stansage" date="1300540645"]Very bad, I found that qjson lib can serialize meta information and member values, but he did not save signals, slots and other meta attributes.[/quote]

    How can you "save a signal"?



  • Well... I guess in theory you could serialize two objects that have a signal/slot connection, and then store that connection as well. However, I know of no public API to enumerate such connections, so that would be hard to do in practice. Also, I think that you need some additional tricks to fully serialize/deserialize QObjects, because you probably also want to keep their parent/child relationships...



  • Possible I can use QObject::connectNotify to obtain objects connections graph.
    Saving signal is just saving signal's normalized signature string.



  • No, you can not, because that event only tells you which objects were connected or disconnected, but not to which signals or which slots. And you really are going to need that information as well.



  • but you can query the meta object for which objects with which slots are connected to which signals. But it could get tricky...



  • [quote author="Gerolf" date="1300560745"]but you can query the meta object for which objects with which slots are connected to which signals. But it could get tricky...[/quote]

    Really? Never to old to learn... I looked over the QMetaObject documentation again, and though I find methods to inspect what signals and slots there are, I don't see methods to see what connections have been made. But perhaps I'm looking in the wrong place. I'm sure such API is available somewhere (Qt's internals know about the connections, obviously), but I never stumbled on it in the public parts. Could you show how you would list the current connections for an object?



  • I thought it is possible, as QMetaObject but not with official API. But to check that and how, I have to dig... could take some time...



  • I think it requires access to QObjectPrivate as this is ultimately what maintains the connection list. So I don't think it is possible to get to it with public API (unless I have overlooked something which is certainly possible).

    You could hack something yourself by using a wrapper function around QObject::connect() where the wrapper maintains a connection list that you can access and still calls the actual QObject::connect.



  • Thanks everybody! I will do experiments and notify about results.



  • Just a short note: I would prefer QDataStream over QTextStream for this purpose. For all the built in Qt types you get a read and a write method, that converts things transparently. On a QTextStream, this information may be lost.



  • This is true. For this reason, QDataStream operators are much simpler to write than the equivalent for QTextStream or QXmlStream[Reader|Writer] since you do not have to do the parsing yourself.

    Writing operator << for Xml output is easy enough but the operator >> can be more work (I've just done a huge pile of these for work) ;-)



  • [quote author="ZapB" date="1300573424"]I think it requires access to QObjectPrivate as this is ultimately what maintains the connection list. So I don't think it is possible to get to it with public API (unless I have overlooked something which is certainly possible).
    [/quote]

    Hi ZapB, you are right, it's only possible by usage of this class, but it is possible. There is no public API for that. And I think, usually you don't meed such things.

    [quote author="ZapB" date="1300573424"]
    You could hack something yourself by using a wrapper function around QObject::connect() where the wrapper maintains a connection list that you can access and still calls the actual QObject::connect.[/quote]

    This would make it even more complex, as mostly you just call connect, without the scope in front. Sou would have to do that for all classes you create.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.