20

I have some problems with reading UTF-8 encoded text from file. My version reads only ASCII characters.

#include <QtCore>

int main()
{
    QFile file("s.txt");

    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        return -1;
    }

    QTextStream in(&file);
    while(!in.atEnd())
    {
        QString line = in.readLine();
        qDebug() << line;
    }
}

s.txt:

jąkać się
ślimak
śnieżyca

output:

"jka si" 
"limak" 
"nieyca"

What should I use?

Maciej Ziarko
  • 11,494
  • 13
  • 48
  • 69

3 Answers3

31

See QTextStream::setCodec():

in.setCodec("UTF-8");
bunto1
  • 331
  • 4
  • 15
John Flatness
  • 32,469
  • 5
  • 79
  • 81
6

You shuold do:

QTextStream in(&file);
in.setCodec("UTF-8"); // change the file codec to UTF-8.

while(!in.atEnd())
{
    QString line = in.readLine();
    qDebug() << line.toLocal8Bit(); // convert to locale multi-byte string 
}
Yi Zhao
  • 6,768
  • 1
  • 18
  • 18
  • After it I get: "j?ka? si?" "?limak" "?nie?yca" – Maciej Ziarko Apr 12 '11 at 03:51
  • 1
    What's your default locale? If `qDebug() << QString("śnieżyca");` works perfectly, it means your system configured a non-ASCII locale. You should try `qDebug() << line.toLocal8Bit();` – Yi Zhao Apr 12 '11 at 05:33
2

I was also having ???? when reading an XML file with QXmlStreamReader. I fixed it by calling this before reading the file:

QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
laurent
  • 88,262
  • 77
  • 290
  • 428