Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Serialize QObject to QString

Serialize QObject to QString

Scheduled Pinned Locked Moved General and Desktop
17 Posts 6 Posters 12.2k Views
  • 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.
  • Z Offline
    Z Offline
    ZapB
    wrote on last edited by
    #4

    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
    0
    • S Offline
      S Offline
      stansage
      wrote on last edited by
      #5

      Yes, it deal tip, thank you!

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dangelog
        wrote on last edited by
        #6

        [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
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #7

          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
          0
          • S Offline
            S Offline
            stansage
            wrote on last edited by
            #8

            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
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #9

              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
              0
              • G Offline
                G Offline
                giesbert
                wrote on last edited by
                #10

                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
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #11

                  [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
                  0
                  • G Offline
                    G Offline
                    giesbert
                    wrote on last edited by
                    #12

                    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
                    0
                    • Z Offline
                      Z Offline
                      ZapB
                      wrote on last edited by
                      #13

                      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
                      0
                      • S Offline
                        S Offline
                        stansage
                        wrote on last edited by
                        #14

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

                        1 Reply Last reply
                        0
                        • G Offline
                          G Offline
                          goetz
                          wrote on last edited by
                          #15

                          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
                          0
                          • Z Offline
                            Z Offline
                            ZapB
                            wrote on last edited by
                            #16

                            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
                            0
                            • G Offline
                              G Offline
                              giesbert
                              wrote on last edited by
                              #17

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

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved