Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. stream.writeQString adds characters in the beginning

stream.writeQString adds characters in the beginning

Scheduled Pinned Locked Moved Solved Qt for Python
7 Posts 3 Posters 888 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.
  • I Offline
    I Offline
    Igor86
    wrote on last edited by
    #1

    Hello!

    stream.writeQString is adding some characters in front of my string.

    \x00\x00\x00$

    why is this happening? can this be removed? I am trying to send some text via TCP and this is messing up the comunication for me.

    my code:

        def sendResult(self, id, crate, program, folddir):
            SIZEOF_UINT16 = 2
            reply = QByteArray()
            stream = QDataStream(reply, QIODevice.WriteOnly)
            stream.setVersion(QDataStream.Qt_4_2)
            stream.writeQString(id + "," + crate + "," + program + "," + folddir)
            stream.device().seek(0)
            stream.writeUInt16(reply.size() - SIZEOF_UINT16)
            self.socket.write(reply)
    

    the blue part is the part that is in eccess..

    9e6dfdc8-11db-40f6-ac6f-d887ac1a8ef6-image.png

    thank you, any help appreciated!

    jsulmJ 1 Reply Last reply
    0
    • I Igor86

      Hello!

      stream.writeQString is adding some characters in front of my string.

      \x00\x00\x00$

      why is this happening? can this be removed? I am trying to send some text via TCP and this is messing up the comunication for me.

      my code:

          def sendResult(self, id, crate, program, folddir):
              SIZEOF_UINT16 = 2
              reply = QByteArray()
              stream = QDataStream(reply, QIODevice.WriteOnly)
              stream.setVersion(QDataStream.Qt_4_2)
              stream.writeQString(id + "," + crate + "," + program + "," + folddir)
              stream.device().seek(0)
              stream.writeUInt16(reply.size() - SIZEOF_UINT16)
              self.socket.write(reply)
      

      the blue part is the part that is in eccess..

      9e6dfdc8-11db-40f6-ac6f-d887ac1a8ef6-image.png

      thank you, any help appreciated!

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Igor86 said in stream.writeQString adds characters in the beginning:

      QDataStream

      it writes some additional data into the stream to be able to read the data from the stream later properly.
      If you use QDataStream to write you also should use it to read the data.
      Else do not use QDataStream.

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

      I 1 Reply Last reply
      2
      • jsulmJ jsulm

        @Igor86 said in stream.writeQString adds characters in the beginning:

        QDataStream

        it writes some additional data into the stream to be able to read the data from the stream later properly.
        If you use QDataStream to write you also should use it to read the data.
        Else do not use QDataStream.

        I Offline
        I Offline
        Igor86
        wrote on last edited by
        #3

        @jsulm said in stream.writeQString adds characters in the beginning:

        @Igor86 said in stream.writeQString adds characters in the beginning:

        QDataStream

        it writes some additional data into the stream to be able to read the data from the stream later properly.
        If you use QDataStream to write you also should use it to read the data.
        Else do not use QDataStream.

        Thank you for your answer, but it is somewhat confusing me. I started coding with python and pyqt5 one week ago, so please bear with me. I found this code in a sample and adapted it for my needs. What should I be using instead of QDataStream to send plain text to a different pc?

        JonBJ 1 Reply Last reply
        0
        • I Igor86

          @jsulm said in stream.writeQString adds characters in the beginning:

          @Igor86 said in stream.writeQString adds characters in the beginning:

          QDataStream

          it writes some additional data into the stream to be able to read the data from the stream later properly.
          If you use QDataStream to write you also should use it to read the data.
          Else do not use QDataStream.

          Thank you for your answer, but it is somewhat confusing me. I started coding with python and pyqt5 one week ago, so please bear with me. I found this code in a sample and adapted it for my needs. What should I be using instead of QDataStream to send plain text to a different pc?

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

          @Igor86

          to send plain text to a different pc

          Send just the text via socket.write(), no QDataStream.

          I also don't know what your writeUInt() is about. Unless the recipient is actually expecting any byte/character count in your protocol.

          I 1 Reply Last reply
          2
          • JonBJ JonB

            @Igor86

            to send plain text to a different pc

            Send just the text via socket.write(), no QDataStream.

            I also don't know what your writeUInt() is about. Unless the recipient is actually expecting any byte/character count in your protocol.

            I Offline
            I Offline
            Igor86
            wrote on last edited by
            #5

            @JonB thank you!

            Like this it worked:

                    out = id + "," + crate + "," + program + "," + folddir
                    self.socket.write(bytearray(out, 'ascii'))
            

            would you mind please explaining for future reference in what situations I would use QDataStream and what the advantages are?

            thank you very much!

            JonBJ 1 Reply Last reply
            0
            • I Igor86

              @JonB thank you!

              Like this it worked:

                      out = id + "," + crate + "," + program + "," + folddir
                      self.socket.write(bytearray(out, 'ascii'))
              

              would you mind please explaining for future reference in what situations I would use QDataStream and what the advantages are?

              thank you very much!

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

              @Igor86
              As @jsulm said, QDataStream is only of use if you have 2 Qt programs as sender & receiver. Otherwise you can't use it.

              If you do, it provides convenient methods to exchange typed data, not just bytes. It works for integers (cross-platform), strings, lists, .... This is very convenient, but is why there is "extra" information when you look at what it produces. It is opaque to you: it works, but you don't understand its data format.

              It also provides support for utilities like buffering its output and "transactions" to recognise when data has been completely transferred. Though you can write those yourself if you need them outside of QDataStream.

              When you can use it it is "convenient" and "efficient" and "portable". But you can't, because your other end is not using it. You want to exchange just plain text, so you do that yourself without QDataStream's layer

              I 1 Reply Last reply
              2
              • JonBJ JonB

                @Igor86
                As @jsulm said, QDataStream is only of use if you have 2 Qt programs as sender & receiver. Otherwise you can't use it.

                If you do, it provides convenient methods to exchange typed data, not just bytes. It works for integers (cross-platform), strings, lists, .... This is very convenient, but is why there is "extra" information when you look at what it produces. It is opaque to you: it works, but you don't understand its data format.

                It also provides support for utilities like buffering its output and "transactions" to recognise when data has been completely transferred. Though you can write those yourself if you need them outside of QDataStream.

                When you can use it it is "convenient" and "efficient" and "portable". But you can't, because your other end is not using it. You want to exchange just plain text, so you do that yourself without QDataStream's layer

                I Offline
                I Offline
                Igor86
                wrote on last edited by
                #7

                @JonB said in stream.writeQString adds characters in the beginning:

                @Igor86
                As @jsulm said, QDataStream is only of use if you have 2 Qt programs as sender & receiver. Otherwise you can't use it.

                If you do, it provides convenient methods to exchange typed data, not just bytes. It works for integers (cross-platform), strings, lists, .... This is very convenient, but is why there is "extra" information when you look at what it produces. It is opaque to you: it works, but you don't understand its data format.

                It also provides support for utilities like buffering its output and "transactions" to recognise when data has been completely transferred. Though you can write those yourself if you need them outside of QDataStream.

                When you can use it it is "convenient" and "efficient" and "portable". But you can't, because your other end is not using it. You want to exchange just plain text, so you do that yourself without QDataStream's layer

                very clear! tank you for taking your time to explain!

                1 Reply Last reply
                1

                • Login

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