[Qt-interest] Reading text files in Windows using QTextStream doesn't work with unicode

Nikos Chantziaras realnc at arcor.de
Fri Jan 21 02:01:33 CET 2011


On 01/21/2011 02:51 AM, Ellen Kestrel wrote:
> Reading text from a file saved with UTF-8 encoding, "á" is imported as
> "á", etc.  I am using:
>
> QFile file (filename);
> QTextStream in (&file);
> file.open (QIODevice::ReadOnly | QIODevice::Text);
> QString line = "";
>
> while (!in.atEnd ())  {
>    line = in.readLine ();
> ...
> }

Maybe Windows isn't using UTF-8 as its default encoding?  I suspect it 
uses a Microsoft-specific codepage.  QTextStream is locale aware and 
will use the system's default encoding.  It doesn't matter what the file 
you're reading is using; what matters is what the OS uses.

If (and only if) you know a file is UTF-8, use QTextStream::setCodec() 
and set it to UTF-8.



More information about the Qt-interest-old mailing list