Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QString and german umlauts

Tags:

c++

qt

qstring

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?

like image 965
punkyduck Avatar asked Feb 22 '23 16:02

punkyduck


2 Answers

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
like image 164
RedX Avatar answered Mar 03 '23 23:03

RedX


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.

like image 35
Mike Seymour Avatar answered Mar 04 '23 00:03

Mike Seymour