Qt QString from string - Strange letters

c++, qstring, qt

Solution

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.

Problem

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?

Original source