How to read UTF-8 text from file using Qt?

18,379

Solution 1

See QTextStream::setCodec():

in.setCodec("UTF-8");

Solution 2

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 
}

Solution 3

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"));
Share:
18,379

Related videos on Youtube

Maciej Ziarko
Author by

Maciej Ziarko

Updated on October 11, 2020

Comments

  • Maciej Ziarko
    Maciej Ziarko over 3 years

    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
    Maciej Ziarko about 13 years
    I tried it and now all the non-ASCII characters are replaced with "?".
  • Maciej Ziarko
    Maciej Ziarko about 13 years
    After it I get: "j?ka? si?" "?limak" "?nie?yca"
  • edA-qa mort-ora-y
    edA-qa mort-ora-y about 13 years
    Perhaps your qDebug output console doesn't support those characters. That'd be a reasonable source of the "?". Try showing the string in a QLabel on-screen.
  • Maciej Ziarko
    Maciej Ziarko about 13 years
    It's not the issue. dDebug() << QString("śnieżyca"); works perfectly.
  • John Flatness
    John Flatness about 13 years
    You're sure your input file is actually UTF-8?
  • Maciej Ziarko
    Maciej Ziarko about 13 years
    4 text editors (Geany, jEdit, gedit, Kate) say it is UTF-8. And still I have my problem with "?".
  • Yi Zhao
    Yi Zhao about 13 years
    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();
  • Maciej Ziarko
    Maciej Ziarko about 13 years
    OK, it eventually fixed my problem. Thanks!

Related