[Qt-interest] Reading text files in Windows using QTextStream doesn't work with unicode
Ellen Kestrel
dragonflykes at gmail.com
Fri Jan 21 02:21:48 CET 2011
On Thu, Jan 20, 2011 at 7:01 PM, Nikos Chantziaras <realnc at arcor.de> wrote:
> 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.
>
Ahh, thank you, that did work. Does it just misrender the characters if the
file is in the wrong encoding, or does something more problematic happen?
Is there a way to automatically detect (or try to detect) the encoding of a
file?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110120/8a10fb7a/attachment.html
More information about the Qt-interest-old
mailing list