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. Qdatastream overloaded function from a readready slot

Qdatastream overloaded function from a readready slot

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 5 Posters 1.2k Views 1 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.
  • S Offline
    S Offline
    Sunfluxgames
    wrote on last edited by Sunfluxgames
    #1

    I'm a little bit confused in how to use the Qdatastream operators or read and write data to a socket.

    struct Science { int abby , int becky };
    
    QDataStream &operator<<(QDataStream &stream, const Science &s) {
        stream << s.abby << s.becky;
        return stream;
    }
    
    QDataStream &operator<<(QDataStream &stream, Science &s) {
        stream >> s.abby >> s.becky;
        return stream;
    }
    

    how do i call these operators to fill in the data once i call my qdatastream from readready slot?

    Anyone have a good full example of ready data from the socket then de-serialize it to a operator then serialize it back to the write socket.

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      // serialise

      QDataStream stream(iodevice);
      Science variable;
      stream << variable;
      

      // deserialise

      QDataStream stream(iodevice);
      Science variable;
      stream >> variable;
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      3
      • S Offline
        S Offline
        Sunfluxgames
        wrote on last edited by
        #3
        void ServerThread::readyRead()
        {
             QDataStream in(socket);
              Science variable;
        
             in.startTransaction();
             in >> variable;
            if(in.commitTransaction())
            {
              // All data necessary was received
           }
        // proccess data here
        }
        

        What do i do about padding? how can i make it dynamic to de-serialize base on whatever structure was received.

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Sunfluxgames said in Qdatastream overloaded function from a readready slot:

          What do i do about padding?

          What do you mean with this? QDataStream is writing the data in it's own format so why do you want to pad something?

          how can i make it dynamic to de-serialize base on whatever structure was received.

          You need to distinguish between the different structures by e.g. a type field.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          2
          • S Offline
            S Offline
            Sunfluxgames
            wrote on last edited by
            #5

            The receiving stream comes in a padding form. The client expects it to be padded.

            Other than align the structure to be padded i guess?

            JonBJ 1 Reply Last reply
            0
            • S Sunfluxgames

              The receiving stream comes in a padding form. The client expects it to be padded.

              Other than align the structure to be padded i guess?

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @Sunfluxgames
              If @Christian-Ehrlicher is correct when he says

              QDataStream is writing the data in it's own format

              and you seem to want to read from/write to an external non-Qt source (right?) with its own "padding"/format, then I would not think QDataStream can be used? It sounds more like you need to do your own char-by-char reading/writing of this Science class? Similarly, how are your ints to be passed between client & server?

              1 Reply Last reply
              3
              • S Offline
                S Offline
                Sunfluxgames
                wrote on last edited by Sunfluxgames
                #7

                Yes it is a non-QT source. My original source does a custom read/write without QDataStreams. I was just trying to see if I could use QDataStreams instead.

                Only thing I can think of is once it enters the QDataStream is to strip the padding and use it like a normal QDataStream.

                Build the reading Structure to fit the need of whatever type field I choose. Build the writing Structure to fit the needed size of whatever the non-QT source wants.

                Something like this possible maybe?

                typedef struct __atribute__((packed)) Science {
                    quint32 abby;
                    quint32 becky;
                } Science;
                
                M 1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  As @JonB already told you you can't use QDataStream for your task and have to write the decoding by your own.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  2
                  • S Sunfluxgames

                    Yes it is a non-QT source. My original source does a custom read/write without QDataStreams. I was just trying to see if I could use QDataStreams instead.

                    Only thing I can think of is once it enters the QDataStream is to strip the padding and use it like a normal QDataStream.

                    Build the reading Structure to fit the need of whatever type field I choose. Build the writing Structure to fit the needed size of whatever the non-QT source wants.

                    Something like this possible maybe?

                    typedef struct __atribute__((packed)) Science {
                        quint32 abby;
                        quint32 becky;
                    } Science;
                    
                    M Offline
                    M Offline
                    mpergand
                    wrote on last edited by mpergand
                    #9

                    @Sunfluxgames said in Qdatastream overloaded function from a readready slot:

                    Something like this possible maybe?
                    typedef struct atribute((packed)) Science {
                    quint32 abby;
                    quint32 becky;
                    } Science;

                    Just have a try !

                    As an old C programmer, i like to fiddle around with bytes :)
                    Let's go:

                    struct __attribute__((packed)) Science
                    {
                        quint8  a;
                        quint16 b;
                        quint8  c;
                    };
                    
                    QByteArray arr=QByteArray::fromHex("01 01 04 0a");
                    QDataStream stream(arr);
                    qint16 b;
                    qint8 a,c;
                    stream>>a>>b>>c;
                    

                    in the degugger i see:
                    arr "\001\001\004\n" QByteArray
                    a 1 qint8
                    b 260 qint16
                    c 10 qint8

                    So far so good.

                    and now with the struct;

                    QDataStream &operator>>(QDataStream &stream, Science &s) {
                        stream >> s.a >> s.b>>s.c;
                        return stream;
                    }
                    
                    QByteArray arr=QByteArray::fromHex("01 01 04 0a");
                    QDataStream stream(arr);
                    Science sc;
                    stream>>sc;
                    

                    arr "\001\001\004\n" QByteArray
                    sc @0x7fff5fbffb48 Science
                    a 1 quint8
                    b 260 quint16
                    c 10 quint8

                    Well, everything is ok.
                    The main question is that 100% sure/reliable ?
                    That's your own choice ;)
                    (note: the values are decoded in big endian)

                    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