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 to return QByteArray set
QtWS25 Last Chance

QDataStream to return QByteArray set

Scheduled Pinned Locked Moved Solved General and Desktop
qbytearrayqdatastreamqt6
11 Posts 4 Posters 1.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.
  • K Offline
    K Offline
    Kevin470
    wrote on 7 Dec 2022, 10:31 last edited by
    #1

    Is there a possibility to find which QByteArray is set to the QDataStream object?
    This method QDataStream::device() returns the device set to it. But is there a way to return if the QDataStream object is connected to a QByteArray and finding out which QByteArray it is set to?
    Thanks.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on 7 Dec 2022, 11:19 last edited by ChrisW67 12 Jul 2022, 11:20
      #2

      I do not know why you would want to do this. Your code constructed the QDataStream with an associated QByteArray so I am not sure how you would not know which byte array that was.

      Anyway, something like this might work:

      QBuffer *internalBuffer = qobject_cast<QBuffer*>(stream.device());
      if (internalBuffer) {
        QByteArray data = internalBuffer->buffer();
        ...
      } 
      else {
        // not  an underlying QByteArray.  Panic?
      }
      
      K 3 Replies Last reply 7 Dec 2022, 12:32
      1
      • C ChrisW67
        7 Dec 2022, 11:19

        I do not know why you would want to do this. Your code constructed the QDataStream with an associated QByteArray so I am not sure how you would not know which byte array that was.

        Anyway, something like this might work:

        QBuffer *internalBuffer = qobject_cast<QBuffer*>(stream.device());
        if (internalBuffer) {
          QByteArray data = internalBuffer->buffer();
          ...
        } 
        else {
          // not  an underlying QByteArray.  Panic?
        }
        
        K Offline
        K Offline
        Kevin470
        wrote on 7 Dec 2022, 12:32 last edited by
        #3

        @ChrisW67 The idea was that I want to write a function that needs the QDataStream and the size of the QByteArray that it is connected to. An example would be like this:

        void sampleFn(QDataStream &ds, QByteArray &ba)
        {
            for (int i = 0; i < ba.size() ; i++)
            {
                qint8 int_8;
                ds >> int_8;
                qInfo() << int_8;
            }
        }
        

        This is just a sample function. I just want to make sure that the user of this function does not give the wrong QByteArray here, so that I can take just one parameter QDataStream &ds and work with it.

        1 Reply Last reply
        0
        • C ChrisW67
          7 Dec 2022, 11:19

          I do not know why you would want to do this. Your code constructed the QDataStream with an associated QByteArray so I am not sure how you would not know which byte array that was.

          Anyway, something like this might work:

          QBuffer *internalBuffer = qobject_cast<QBuffer*>(stream.device());
          if (internalBuffer) {
            QByteArray data = internalBuffer->buffer();
            ...
          } 
          else {
            // not  an underlying QByteArray.  Panic?
          }
          
          K Offline
          K Offline
          Kevin470
          wrote on 7 Dec 2022, 12:59 last edited by
          #4

          @ChrisW67 And also, this works! Thank you so much.

          1 Reply Last reply
          0
          • C ChrisW67
            7 Dec 2022, 11:19

            I do not know why you would want to do this. Your code constructed the QDataStream with an associated QByteArray so I am not sure how you would not know which byte array that was.

            Anyway, something like this might work:

            QBuffer *internalBuffer = qobject_cast<QBuffer*>(stream.device());
            if (internalBuffer) {
              QByteArray data = internalBuffer->buffer();
              ...
            } 
            else {
              // not  an underlying QByteArray.  Panic?
            }
            
            K Offline
            K Offline
            Kevin470
            wrote on 7 Dec 2022, 14:24 last edited by
            #5

            @ChrisW67 Is it possible to get the pointer or reference of the QByteArray corresponding to the QDataStream?

            C C 2 Replies Last reply 7 Dec 2022, 16:14
            0
            • K Kevin470
              7 Dec 2022, 14:24

              @ChrisW67 Is it possible to get the pointer or reference of the QByteArray corresponding to the QDataStream?

              C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 7 Dec 2022, 16:14 last edited by
              #6

              @Kevin470 said in QDataStream to return QByteArray set:

              Is it possible to get the pointer or reference of the QByteArray corresponding to the QDataStream?

              Why would someone need it? Why do you want to modify the data in a function in a function where it should not be done? What's the (strange) use case for this?

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

              K 1 Reply Last reply 8 Dec 2022, 08:51
              2
              • K Kevin470
                7 Dec 2022, 14:24

                @ChrisW67 Is it possible to get the pointer or reference of the QByteArray corresponding to the QDataStream?

                C Offline
                C Offline
                ChrisW67
                wrote on 7 Dec 2022, 21:54 last edited by
                #7

                @Kevin470 That is what the code I gave you does. Not sure I understand the question (or, as @Christian-Ehrlicher and I have said, the original problem).

                I just want to make sure that the user of this function does not give the wrong QByteArray here, so that I can take just one parameter QDataStream &ds and work with it.

                You can derive the QByteArray, and hence its size, inside the function.
                The function is misleading though; you cannot pass an arbitrary QDataStream and expect the function to work.

                This should fly and does not need to know the size or type of the underlying data source:

                void sampleFn(QDataStream &ds)
                {
                    while (!ds.atEnd()) 
                    {
                        qint8 int_8;
                        ds >> int_8;
                        qInfo() << int_8;
                    }
                }
                

                Have you considered passing the function the QByteArray by const reference? If the stream in the array is complex you can still do something like this:

                void sampleFn(const QByteArray &ba)
                {
                    QDataStream ds(ba);
                    while (!ds.atEnd()) 
                    {
                        qint8 int_8;
                        qint32 int_32;
                        ds >> int_8 >> int_32;
                        qInfo() << int_8 << int_32;
                    }
                }
                

                If you are using QDataStream just to read bytes from the buffer then you can do without QDataStream entirely.

                1 Reply Last reply
                1
                • C Christian Ehrlicher
                  7 Dec 2022, 16:14

                  @Kevin470 said in QDataStream to return QByteArray set:

                  Is it possible to get the pointer or reference of the QByteArray corresponding to the QDataStream?

                  Why would someone need it? Why do you want to modify the data in a function in a function where it should not be done? What's the (strange) use case for this?

                  K Offline
                  K Offline
                  Kevin470
                  wrote on 8 Dec 2022, 08:51 last edited by
                  #8

                  @Christian-Ehrlicher @ChrisW67 I have not found a right solution to my problem yet.
                  My actual use case is to read and write strings to a QByteArray but without the extra information of the string size and everything else in it.
                  For example if I am to write a QString str = "Hello World" to a QByteArray, I am trying to find a solution to write just the bytes of each character in the string in the QByteArray.
                  Which means, it must contain only 48:65:6C:6C:6F:20:57:6F:72:6C:64 and not 00:00:00:0B:48:65:6C:6C:6F:20:57:6F:72:6C:64 where 0B gives the size of the String.
                  Similarly, the other way around. To read each byte to a QString without the need of knowing the size. Because the size is already known during reading and writing.

                  I was thinking of maybe manipulating the QByteArray so that the bytes containing the size of the QString is removed. But I know it is a very bad idea to do so. That is when I was asking if it is possible to know which QByteArray is corresponding with the QDataStream.

                  Is there a possibility or workarounds for my use case?

                  J 1 Reply Last reply 8 Dec 2022, 08:56
                  0
                  • K Kevin470
                    8 Dec 2022, 08:51

                    @Christian-Ehrlicher @ChrisW67 I have not found a right solution to my problem yet.
                    My actual use case is to read and write strings to a QByteArray but without the extra information of the string size and everything else in it.
                    For example if I am to write a QString str = "Hello World" to a QByteArray, I am trying to find a solution to write just the bytes of each character in the string in the QByteArray.
                    Which means, it must contain only 48:65:6C:6C:6F:20:57:6F:72:6C:64 and not 00:00:00:0B:48:65:6C:6C:6F:20:57:6F:72:6C:64 where 0B gives the size of the String.
                    Similarly, the other way around. To read each byte to a QString without the need of knowing the size. Because the size is already known during reading and writing.

                    I was thinking of maybe manipulating the QByteArray so that the bytes containing the size of the QString is removed. But I know it is a very bad idea to do so. That is when I was asking if it is possible to know which QByteArray is corresponding with the QDataStream.

                    Is there a possibility or workarounds for my use case?

                    J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 8 Dec 2022, 08:56 last edited by
                    #9

                    @Kevin470 said in QDataStream to return QByteArray set:

                    My actual use case is to read and write strings to a QByteArray but without the extra information of the string size and everything else in it.

                    Then you should not use QDataStream at all.
                    QString has some methods to convert a QByteArray to a QString (for example https://doc.qt.io/qt-6/qstring.html#fromUtf8-2).
                    It also has methods to convert a QString to a QByteArray: https://doc.qt.io/qt-6/qstring.html#toUtf8

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    K 1 Reply Last reply 8 Dec 2022, 09:31
                    2
                    • J jsulm
                      8 Dec 2022, 08:56

                      @Kevin470 said in QDataStream to return QByteArray set:

                      My actual use case is to read and write strings to a QByteArray but without the extra information of the string size and everything else in it.

                      Then you should not use QDataStream at all.
                      QString has some methods to convert a QByteArray to a QString (for example https://doc.qt.io/qt-6/qstring.html#fromUtf8-2).
                      It also has methods to convert a QString to a QByteArray: https://doc.qt.io/qt-6/qstring.html#toUtf8

                      K Offline
                      K Offline
                      Kevin470
                      wrote on 8 Dec 2022, 09:31 last edited by Kevin470 12 Aug 2022, 09:34
                      #10

                      @jsulm Therein lies my problem unfortunately. (The actual source of the bytearray that I receive is actually not written using the Qt Framework, and so it does not have any QString QByteArray concepts in it, so I have to read every byte separately in a uniform order and strings do not have their size mentioned before them)
                      My QByteArray consists of data from different datatypes.
                      For example in this instance:

                      QByteArray ba;
                      QDataStream write(&ba,QDataStream::WriteOnly);
                      QDataStream read(&ba,QDataStream::ReadOnly);
                      
                      quint8 int8 = 14;
                      quint16 int16 = 22;
                      write << int8;
                      write << int16;
                      
                      qInfo() << QString("Hello World").toUtf8().toHex(':');
                      write << QString("Hello World").toUtf8();
                      
                      qInfo() << ba.toHex(':');
                      

                      Here I expect "0e:00:16:48:65:6c:6c:6f:20:57:6f:72:6c:64" and not "0e:00:16:00:00:00:0b:48:65:6c:6c:6f:20:57:6f:72:6c:64"
                      I can't use QByteArray::append() because the flexibility to choose the Endianness is lost I think. And anyway, append makes the quint16 to single byte if the number can be written in a single byte.

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        Kevin470
                        wrote on 8 Dec 2022, 11:10 last edited by
                        #11

                        I made a rudimentary solution for the problem.

                        //Read data to QString with a known length
                        void readDataQString(QDataStream &ds, QString &str, uint length)
                        {
                            QByteArray buffer(length,Qt::Uninitialized);
                            ds.readRawData(buffer.data(),length);
                            str = QString(buffer);
                            qInfo() << str;
                        }
                        
                        //Write data from QString without length info
                        void writeDataQString(QDataStream &ds, QString &str)
                        {
                            QByteArray ab = str.toUtf8();
                            for(int i = 0; i < ab.size(); ++i)
                            {
                                quint8 int_8 = static_cast<quint8>(ab.at(i));
                                ds << int_8;
                            }
                            QBuffer *internalBuffer = qobject_cast<QBuffer*>(ds.device());
                            QByteArray ba = internalBuffer->buffer();
                            qInfo() << ba.toHex(':');
                        }
                        

                        I hope this works for everyone.

                        1 Reply Last reply
                        0

                        8/11

                        8 Dec 2022, 08:51

                        • Login

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