4

Whenever I try to convert a std::string into a QString with this letter in it ('ß'), the QString will turn into something like "Ã" or some other really strange letters. What's wrong? I used this code and it didn't cause any errors or warnings!

std::string content = "Heißes Teil.";
ui->txtFind_lang->setText(QString::fromStdString(content));

The std::string has no problem with this character. I even wrote it into a text file without problems. So what am I doing wrong?

2 Answers 2

6

You need to set the codec to UTF-8 :

QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));

By default, Qt uses the Latin-1 encoding, which is limited. By adding this code, you set the default encoding to UTF-8 which allow you to use much more characters.

Sign up to request clarification or add additional context in comments.

1 Comment

I edited the answer to add explanations. By the way, could you mark this answer as the accepted answer if it helped you?
1

Though antoyo's answer works, I wasn't too sure why. So, I decided to investigate.

All of my documents are encoded in UTF-8, as are most web-pages. The ß character has the UTF code point of UTF+00DF.

Since UTF-8 is a variable length encoding, in the binary form, ß would be encoded as 11000011 10011111 or C3 9F. Since by default Qt relies on Latin1 encoding. It would read ß as two different characters. The first one C3 will map to à and the second one 9F will not map to anything as Latin1 does not recognize bytes in between 128-159 (in decimal).

That's why ß appears as à when using Latin1 encoding.


Side note: You might want to brush up on how UTF-8 encoding works, because otherwise it seems a little unintuitive that ß takes two bytes even though its code point DF is less than FF and should consume just one byte.

Comments

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.