Important: Please read the Qt Code of Conduct -

[Solved] Problem with tr an ä,ü,ö

  • Hello,

    I want to change the Text of a label
    with ui.llabel->changeText(tr("test1")) and also with
    words that contain a ä,ü or ö (I don't know the english word
    for this letters). But then I get some other letters.
    And if I try QString::fromUtf8(tr("test1")), that don't work,
    because the fromUtf8 function wants a const string.

  • Moderators

    How about:

    Or simply use the UTF chars with tr, without any reference to QString.

  • tr() wants a const QString and fromUtf8 returns a QString.
    Casting also dont work.

  • Moderators

    Are your source files encoded in UTF-8? Maybe that is why you used to get "then I get some other letters".

    I guess Volker would be better here, he has experience with tr() and UTF-8. I usually code in English only.

  • You have to make sure your files are encoded in UTF-8.

    Also add following:

    here is part of my main.cpp:

    #include <qtextcodec.h>

    int main(int argc, char *argv[])

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

  • Hm, usually the non-ASCII characters should come in with the translation files, and not stand hardcoded in the source file. So write your hardcoded strings in pure ASCII in the source file. If any translation (even the english one) needs non-ASCII characters somewhere, do it with the translation file. Until then it is sensible to replace ä ö ü with ae oe ue and tell the translator(s) to make sure they convert them to umlauts (<- that's the english word for them).

    If you insist on using non-ASCII characters in your source file, use trUtf8. Maybe a better alternative would be to escape them and use regular tr. For example tr("\304") will return Ä. (That's the octal code for the UTF-8-character Ä).
    Those are the codes in hex (0x00C4 = 304 (octal)):
    Ä U+00C4
    Ö U+00D6
    Ü U+00DC
    ä U+00E4
    ö U+00F6
    ü U+00FC

    //EDIT: @Yucel: Well if you use UTF-8 Encoding (which is highly recommended) you shouldn't set your codecs to ISO-8859-9 but to UTF-8. Also on Windows.

  • you are right, I used it as an example from one of my projects. Should have replaced it, before posting.

  • thank you for your responses. I tried it with trUtf8 and
    it works fine.

Log in to reply