QT5 and ä, ö, ü (Umlaut)



  • Hi,

    I have just chosen to update from QT4 to QT5. Unfortunately, all äs, ös and üs (Umlaute) do not show up properly. I then checked out the posts in the forum but none of the proposed solutions solved my issue.

    Here is what I did with a QLabel, assuming that "QLabel* label = new QLabel;" is somewhere else.

    label->setText("ä"); ---> does not show any ä

    QString tt = tr( "äöü");
    label->setText(tt); ---> does not show any ä

    QTextCodec *codec = QTextCodec::codecForName("UTF-8");
    QString tt = codec->toUnicode( "äöü");
    label->setText(tt); ---> does not show any ä

    QString tt = QString::fromUtf8( "äöü");
    label->setText(tt); ---> does not show any ä

    Has anybody got any other idea or is this just a bug? Things worked without any problem with QT4..

    I tried also to do it as the designer generated code does but designer generates ascii codes (\303)
    directly which is not an option since I get some data to show from non-QT subroutines.

    I use Microsoft Visual Studio 2012 (which should be UTF-8 ) and Unicode, QT 5.1.1 x86 (32 bit) downloaded as the binary package from the web page.

    Thank you for any help

    Hauke


  • Moderators

    Hi,

    I haven't tried this myself (I'm not at a machine with VS 2012), but try marking your strings as Unicode literals:

    @
    QString tt(u"äöü");

    // OR
    QString tt = QStringLiteral("äöü");
    @



  • Hi,

    thank you, that helped: QStringLiteral has been the working solution. u"äüö" is only for gcc - is my feeling, I did not really investigate it but compiler complained about it.

    But this brings me to the follow-up question: What if I want to do the following:

    Instead of
    ->
    QString tt = QStringLiteral("ä");

    I would like to do it
    ->
    std::string str = "ä";
    QString tt = QStringLiteral(str.c_str());

    This does not work with QStringLiteral as it produces a compiler error message. This situation is very common, e.g., I am working with a german PC and list up audio devices with portaudio, the name "primärer Audiotreiber" is returned as a std::string or const char*.

    Thank you and best regards

    Hauke


  • Moderators

    Hrmm... petition Microsoft to hurry up and implement Unicode string literal support? :-/ "u" is in the C++11 standard, but MSVC 2012 doesn't support it: http://blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.aspx

    From "this blog":http://msdn.microsoft.com/en-us/library/vstudio/69ze775t.aspx, L"äöü" might work in MSVC.

    [quote]
    @
    std::wstring str = “ä”; // EDIT: wstring, not string
    @
    [/quote]Make sure you never write code that does that. std::string stores an array of bytes. Unicode characters can be multi-byte.

    You can probably successfully store and retrieve unicode bytes like this...
    @
    std::string str = L"ä";
    @
    ... but the "ä" character is 2 bytes long, so str.length() == 2!

    See http://stackoverflow.com/questions/3257263/how-do-i-get-stl-stdstring-to-work-with-unicode-on-windows

    [quote]
    @
    QString tt = QStringLiteral(str.c_str());
    @
    [/quote]That is a compilation error because QStringLiteral is a macro that constructs a QString at compile-time, but str.c_str() can only be evaluated at run-time.

    Use one of the QString::from***() functions (depending on how your string was encoded).

    ================
    In case you're wondering why QStringLiteral behaves this way, consider these two strings:
    @
    QString str1 = "abc";
    QString str2(QStringLiteral("abc"));
    @

    str1 requires data conversion at run-time, but str2 does not (because the QString was created at compile-time). Thus, str2 is more efficient -- this is why QStringLiteral exists.

    P.S. Please add "@" before and after your code segments. It makes it much easier to read.



  • Hi, thank you for your answer. I think I have understood the problem now - what I need is the Latin1 encoding for the ASCII Bytes. I did not even know that it is Latin1. To use std::string as described worked for many years without any problem for me ...

    Let me just comment on two things:

    @
    std::string str = "äü";
    @

    str.size() gives me a 2.

    In order to use L"ä", I would need to use std::wstring instead of std::string. Otherwise I get a compiler error.

    QString::fromLatin1 seems to have solved all problems.

    Thank you and have a nice weekend

    Hauke


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.