Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Serialize QObject to QString

    General and Desktop
    6
    17
    11045
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      stansage last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • G
        giesbert last edited by

        I know no other way for that, sorry

        Nokia Certified Qt Specialist.
        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

        1 Reply Last reply Reply Quote 0
        • S
          stansage last edited by

          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.

          1 Reply Last reply Reply Quote 0
          • Z
            ZapB last edited by

            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.

            Nokia Certified Qt Specialist
            Interested in hearing about Qt related work

            1 Reply Last reply Reply Quote 0
            • S
              stansage last edited by

              Yes, it deal tip, thank you!

              1 Reply Last reply Reply Quote 0
              • D
                dangelog last edited by

                [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"?

                Software Engineer
                KDAB (UK) Ltd., a KDAB Group company

                1 Reply Last reply Reply Quote 0
                • A
                  andre last edited by

                  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...

                  1 Reply Last reply Reply Quote 0
                  • S
                    stansage last edited by

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

                    1 Reply Last reply Reply Quote 0
                    • A
                      andre last edited by

                      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.

                      1 Reply Last reply Reply Quote 0
                      • G
                        giesbert last edited by

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

                        Nokia Certified Qt Specialist.
                        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                        1 Reply Last reply Reply Quote 0
                        • A
                          andre last edited by

                          [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?

                          1 Reply Last reply Reply Quote 0
                          • G
                            giesbert last edited by

                            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...

                            Nokia Certified Qt Specialist.
                            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                            1 Reply Last reply Reply Quote 0
                            • Z
                              ZapB last edited by

                              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.

                              Nokia Certified Qt Specialist
                              Interested in hearing about Qt related work

                              1 Reply Last reply Reply Quote 0
                              • S
                                stansage last edited by

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

                                1 Reply Last reply Reply Quote 0
                                • G
                                  goetz last edited by

                                  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.

                                  http://www.catb.org/~esr/faqs/smart-questions.html

                                  1 Reply Last reply Reply Quote 0
                                  • Z
                                    ZapB last edited by

                                    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) ;-)

                                    Nokia Certified Qt Specialist
                                    Interested in hearing about Qt related work

                                    1 Reply Last reply Reply Quote 0
                                    • G
                                      giesbert last edited by

                                      [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.

                                      Nokia Certified Qt Specialist.
                                      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                                      1 Reply Last reply Reply Quote 0
                                      • First post
                                        Last post