Stalyon
Stalyon

Reputation: 578

Converting QString from string in a file

I want to set a text in a QLabel so I need to use a QString. But I read a file and the text contains accents. I tried with QString::fromUtf8() but it doesn't work.

Any idea?

string line;
QString lineTranslate;
getline(file, line);
lineTranslate = QString::fromStdString(line);
m_nomCourant->setText(QString::fromLatin1("<u><strong>Nom courant :</strong></u> ") + lineTranslate);

Desired output:

Nom courant : Requin
Nom scientifique : Carcharhinus menalopterus

Habitat : Côtier / Dans les zones coralliennes jusqu'à -30m
Famille : Carcharhinidés

Actual output:

Nom courant : Requin
Nom scientifique : Carcharhinus menalopterus

Habitat : C?tier / Dans les zones coralliennes jusqu'? -30m
Famille : Carcharhinid?s

Edit: What do you advise me to use to have a QString with several lines?

Upvotes: 2

Views: 259

Answers (1)

Googie
Googie

Reputation: 6017

You need to know what encoding (charset) is used in your file. Then you will either use fromUtf8, or something else - using the QTextCodec.

Example from Qt docs:

QTextCodec *codec = QTextCodec::codecForName("Shift-JIS");
QTextDecoder *decoder = codec->makeDecoder();

QString string;
while (new_data_available()) {
    QByteArray chunk = get_new_data();
    string += decoder->toUnicode(chunk);
}
delete decoder;

Upvotes: 3

Related Questions