[Qt-interest] Encoding problem on WindowsXP/Qt4.6.2

Jeffrey Brendecke jwbrendecke at icanetix.com
Mon Sep 13 08:29:58 CEST 2010


I am having a problem with encoding on a stream read from a process.

The data is generated from a Python script, which writes to stdout as binary 
data. I can redirect the output to a file and open it in VS 2005 as UTF-8 
without BOM and everything is fine:

script.py > tmp.txt

However, when I read the process from the Qt app as follows, I get garbage for 
characters past ASCII. Here is the code:

bool
getData( const QString& cmd, const QStringList& args, QStringList& lines )
{
  QTextStream stream;
  stream.setCodec( "UTF-8" );
  
  QProcess proc;
  stream.setDevice( &proc );
  proc.setProcessChannelMode( QProcess::MergedChannels );
  proc.start( cmd, args, QIODevice::ReadOnly );
  bool result = proc.waitForFinished();
  if ( !result )
  {
    return result;
  }

  proc.closeReadChannel( QProcess::StandardOutput );

  while( true )
  {
    QString line( stream.readLine() );
    if ( line.isNull() )
    {
      break;
    }
    // Don't trim off trailing tab!
    QString testLine( line.trimmed() );
    if ( testLine.length() > 0 )
    {
      lines << line;
    }
  }
  return result;
}

It suspect that QProcess is reading the data as text using the local 8-bit 
encoding, which is resulting in the garbage.

How can I fix this problem?

Thanks in advance!

Sincerely,

Jeffrey Brendecke

Managing Director
icanetix Software Systems and Consulting GmbH
Untere Hagenstrasse 26
91217 Hersbruck
Germany

Commercial Registry B 25317 Nuremberg, Germany
VAT-ID: DE250213502



More information about the Qt-interest-old mailing list