Qt QString from a string - Strange letters

Whenever I try to convert std::string to a QString with that 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 did not cause any errors or warnings!

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

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

+4
source share
2 answers

You need to install the codec in UTF-8:

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

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

+6
source

Although antoyo answer worked, I was not too sure about that. So, I decided to research.

All my documents are encoded in UTF-8 , like most web pages. The symbol ß has a code point UTF UTF + 00DF .

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

This is why ß displayed as Γƒ when using Latin1 encoding.


Note. Maybe you should figure out how UTF-8 encoding works, because otherwise it seems a little unintuitive that ß takes two bytes, although its DF code point is less than FF and should only consume one byte.

+1
source

All Articles