Pergunta

I am working with C++ and QT and have a problem with german umlauts. I have a QString like "wir sind müde" and want to change it to "wir sind müde" in order to show it correctly in a QTextBrowser.

I tried to do it like this:

s = s.replace( QChar('ü'), QString("ü"));

But it does not work.

Also

 s = s.replace( QChar('\u00fc'), QString("ü"))

does not work.

When I iterate through all characters of the string in a loop, the 'ü' are two characters.

Can anybody help me?

Foi útil?

Solução

QStrings are UTF-16.

QString stores a string of 16-bit QChars, where each QChar corresponds one Unicode 4.0 character. (Unicode characters with code values above 65535 are stored using surrogate pairs, i.e., two consecutive QChars.)

So try

//if ü is utf-16, see your fileencoding to know this
s.replace("ü", "ü")

//if ü if you are inputting it from an editor in latin1 mode
s.replace(QString::fromLatin1("ü"), "ü");
s.replace(QString::fromUtf8("ü"), "ü"); //there are a bunch of others, just make sure to select the correct one

Outras dicas

There are two different representations of ü in Unicode:

  • The single point 00FC (LATIN SMALL LETTER U WITH DIAERESIS)
  • The sequence 0075 (LATIN SMALL LETTER U) 0308 (COMBINING DIAERESIS)

You should check for both.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top