<html>
  <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <p>Hi. Are you looking for:</p>
    <p>stream = qt.QDataStream(byte_array, qt.QIODevice.ReadOnly)<br>
      my_string = stream.readQString()</p>
    <p>and</p>
    <p>stream = qt.QDataStream(byte_array, qt.QIODevice.WriteOnly)<br>
      stream.writeQString(my_string)</p>
    <p>where byte_array is a QByteArray</p>
    <p>In my experience it looks to be portable, you dont have to
      encode/decode the string. You only have to play with unicode
      python strings.<br>
    </p>
    <p>Regards,<br>
    </p>
    <br>
    <div class="moz-cite-prefix">On 11/30/2017 12:02 PM, J Barchan
      wrote:<br>
    </div>
    <blockquote
cite="mid:CABz3M__WzrbN8xs8qLxgNS37E2rb_-LqNeywnu8npTck9CiCPg@mail.gmail.com"
      type="cite">
      <div dir="ltr">D
        <div style="font-family:tahoma,sans-serif;display:inline"
          class="gmail_default">​ear, helpful PyQt/Python experts,</div>
        <div style="font-family:tahoma,sans-serif;display:inline"
          class="gmail_default"><br>
        </div>
        <div style="font-family:tahoma,sans-serif;display:inline"
          class="gmail_default"><br>
        </div>
        <div style="font-family:tahoma,sans-serif;display:inline"
          class="gmail_default">I have a problem, receiving a <span
            style="font-family:monospace,monospace">​UnicodeDecodeError</span>. 
          The full details are at <a moz-do-not-send="true"
href="https://forum.qt.io/topic/85493/unicodedecodeerror-with-output-from-windows-os-command">https://forum.qt.io/topic/85493/unicodedecodeerror-with-output-from-windows-os-command</a>,
          in combination with <a moz-do-not-send="true"
href="https://forum.qt.io/topic/85064/python3-pyqt5-x-qbytearray-to-string">https://forum.qt.io/topic/85064/python3-pyqt5-x-qbytearray-to-string</a>. 
          Here I will try to be brief to cover only the salient points.:</div>
        <div style="font-family:tahoma,sans-serif;display:inline"
          class="gmail_default">
          <ol>
            <li>I am spawning a <span
                style="font-family:monospace,monospace">QProcess</span>
              under Windows to run an arbitrary OS command, and grab its
              output to display in a window.</li>
            <li>I use <span style="font-family:monospace,monospace">QByteArray
                Process.readAllStandardOutput()</span> to receive the
              child's output in the parent.</li>
            <li>I wish to display it in a dialog window, so I (believe I
              need to) use <span
                style="font-family:monospace,monospace">QLineEdit.setPlainText(QString)</span>
              to do that.</li>
            <li>To achieve that I will need to convert a <span
                style="font-family:monospace,monospace">QByteArray</span>
              to a <span style="font-family:monospace,monospace">QString</span>.</li>
            <li>Since neither of these types and their methods are
              available in PyQt, I asked how to do that in <a
                moz-do-not-send="true"
href="https://forum.qt.io/topic/85064/python3-pyqt5-x-qbytearray-to-string">https://forum.qt.io/topic/85064/python3-pyqt5-x-qbytearray-to-string</a>.</li>
            <li>The answer I got from a PyQt expert, "jazzycamel", who
              visited there (and told me to come to this forum in
              future, which is why I am here now!), was that the
              Python/PyQt way to do this is to work with its native <span
                style="font-family:monospace,monospace">bytes</span>
              & <span style="font-family:monospace,monospace">str</span>
              types respectively, and use <span
                style="font-family:monospace,monospace">str =
                bytes.decode('utf-8')</span> to "convert".</li>
          </ol>
          <p>While this has worked fine under Linux, when a user runs my
            Qt app under Windows and issues a perfectly normal <span
              style="font-family:monospace,monospace">robocopy</span>
            command under a standard UK Windows with really nothing
            special/unusual going on with filenames, he gets:</p>
          <blockquote class="gmail_quote" style="margin:0px 0px 0px
            0.8ex;border-left:1px solid
            rgb(204,204,204);padding-left:1ex">
            <p><span style="font-family:monospace,monospace">Unhandled
                Exception:<br>
                <br>
                'utf-8' codec can't decode byte 0x9c in position 32:
                invalid start byte<br>
                <br>
                <class 'UnicodeDecodeError'><br>
                File "C:\HJinn\widgets\messageboxes.py", line 289, in
                processReadyReadStandardOutput<br>
                output = output.data().decode('utf-8')</span></p>
          </blockquote>
          <p>I understand this to be a Python/PyQt problem.  I <i>believe</i>
            if I used the native Qt/C++ calls for this --- which
            apparently I can't from PyQt --- there would be no issue.</p>
          <p>I have over the years written, say, Windows C programs
            using standard Windows SDK calls for this kind of
            "redirector".  I simply grab the output of a sub-process and
            throw it at whatever the native Windows <span
              style="font-family:monospace,monospace">SetTextEdit()</span>
            function is, and all has always been fine.</p>
          <p>Note that I have <i>never</i> had to guess/decode/convert
            bytes to some text encoding, and this has worked across all
            platforms forever.  So I really don't expect to have to do
            so now, unless there's something going on in Qt/PyQt which
            is fundamentally any different.</p>
          <p>So... can you please tell me how under Python/PyQt I can
            just display the output from an OS command (assuming
            "text-type" output, I don't expect arbitrary binary bytes)
            without the slightest chance of any kind of "I can't
            convert" Exception, please?</p>
          <p>Thank you in advance!<br>
          </p>
          <p><br>
          </p>
          <p><br>
          </p>
        </div>
        <div style="font-family:tahoma,sans-serif;display:inline"
          class="gmail_default"><br>
        </div>
      </div>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
PyQt mailing list    <a class="moz-txt-link-abbreviated" href="mailto:PyQt@riverbankcomputing.com">PyQt@riverbankcomputing.com</a>
<a class="moz-txt-link-freetext" href="https://www.riverbankcomputing.com/mailman/listinfo/pyqt">https://www.riverbankcomputing.com/mailman/listinfo/pyqt</a></pre>
    </blockquote>
    <br>
  </body>
</html>