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. QByteArray to QObject typecasting

QByteArray to QObject typecasting

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 3.3k Views 2 Watching
  • 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.
  • D Offline
    D Offline
    ddze
    wrote on last edited by
    #1

    It seemed a trivial problem but somehow I did not find an example of casting from QByteArray to a QObject. I have tried several ways like QObject *temp = qobject_cast<QObject *>(QByteArray) .

    kshegunovK 1 Reply Last reply
    0
    • D ddze

      It seemed a trivial problem but somehow I did not find an example of casting from QByteArray to a QObject. I have tried several ways like QObject *temp = qobject_cast<QObject *>(QByteArray) .

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      @ddze
      Well, you can't! These classes are not in the same hierarchy, so neither qobject_cast, nor dynamic_cast will work. In fact QByteArray is a base class, and doesn't depend on the Qt's meta-object system in comparison to QObject.

      Kind regards.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      1
      • D Offline
        D Offline
        ddze
        wrote on last edited by ddze
        #3

        I thought so , thanks kshegunov. Looks like that I will have to overload the operators << and >>.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi and welcome to devnet,

          In addition to what @kshegunov wrote. What are you trying to serialize exactly ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          D 1 Reply Last reply
          0
          • SGaistS SGaist

            Hi and welcome to devnet,

            In addition to what @kshegunov wrote. What are you trying to serialize exactly ?

            D Offline
            D Offline
            ddze
            wrote on last edited by
            #5

            @SGaist

            I am trying to serialize an object to into the QMimeData instance. Still need to get around the the missing operators << >> in the QGraphicsItem class.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              What would you like to do with that object ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • D Offline
                D Offline
                ddze
                wrote on last edited by
                #7

                Transfer it from one place to another.

                kshegunovK 1 Reply Last reply
                0
                • D ddze

                  Transfer it from one place to another.

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by
                  #8

                  @ddze
                  Usually you don't do that, for the same reason that you don't copy QObject instances. If you have some data put into a QObject one usually extracts that data by a method and serializes the data itself, not the QObject. Consider what you're supposed to do to serialize the object hierarchy (with the parents and children) for a QObject instance? Also signals, slots and connections can't be trivially expanded in means of serialization ... there are tons of problems that prevent you from actually copying/serializing (which is basically the same thing) a QObject instance.

                  Kind regards.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  1
                  • D Offline
                    D Offline
                    ddze
                    wrote on last edited by
                    #9

                    @kshegunov and @SGaist ,

                    Thank you.

                    I will probably go with the plan of storing a pointer into the QMimeData instead of streaming in/out the entire structure of the object.

                    1 Reply Last reply
                    0
                    • Hamed.MasafiH Offline
                      Hamed.MasafiH Offline
                      Hamed.Masafi
                      wrote on last edited by Hamed.Masafi
                      #10

                      Your class must implement this operators:

                      QDataStream &operator<<
                      QDataStream &operator>>
                      

                      After that you can use this code:

                      MyClass c;
                      QDataStream reader(byteArray);
                      reader << c;
                      

                      Remote object sharing (OO RPC)
                      http://forum.qt.io/topic/60680/remote-object-sharing-oo-rpc-solved

                      Advanced, Powerful and easy to use ORM for Qt5
                      https://forum.qt.io/topic/67417/advanced-powerful-and-easy-to-use-orm-for-qt5

                      D 1 Reply Last reply
                      0
                      • Hamed.MasafiH Hamed.Masafi

                        Your class must implement this operators:

                        QDataStream &operator<<
                        QDataStream &operator>>
                        

                        After that you can use this code:

                        MyClass c;
                        QDataStream reader(byteArray);
                        reader << c;
                        
                        D Offline
                        D Offline
                        ddze
                        wrote on last edited by ddze
                        #11

                        @Hamed.Masafi

                        I know that and it would be easy if there was no a pointer to a QGraphicItem in the object structure (which has no << , >> ) operators.

                        Also , my class provides the insertion and retraction operators but seems impossible to use them in the MIme as they fail to populate the members of the class such as QByteArray , QGraphicItem ... since these classes do not provide <<>> operators on their own.

                        Looks pretty limited staff that can be transferred with the QMimeData , basic texts, urls, qt-class objects , but the objects from non Qt classes containing the Qt class members with no << >> operators, it is hard or even impossible - since it is requirement that they have << >> in order to use the QDataStream object.

                        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