2

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?

6
  • Could you include your desired and actual output in your question? Commented Apr 28, 2015 at 7:08
  • img11.hostingpics.net/pics/858012Capture.png Commented Apr 28, 2015 at 7:12
  • If you want several lines and are using html, just use <br/> Commented Apr 28, 2015 at 7:22
  • Thx Morb. But it will better if it is automatic (when the QString is too long for the QLabel for exemple). Commented Apr 28, 2015 at 7:26
  • I think this question can help you with this other problem stackoverflow.com/questions/12281396/qlabel-auto-multiple-lines Commented Apr 28, 2015 at 7:31

1 Answer 1

3

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;
Sign up to request clarification or add additional context in comments.

1 Comment

Thx for your answer. I didn't think to that. So I changed the encoding of my file to Utf8. And it works with my last code. Thx Googie.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.