Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Character encoding problem



  • Hi!

    I have some hungarian text stored in mysql db. The insertion is done from a windows program, it works with wchar_t strings. Before insert a string, I convert the data with WideCharToMultibyte() function, because the database's character set is UTF8, after select, I have back the original string with MultiByteToWideChar() function.
    How can I convert a string stored in the database to display in a QLabel, for example the hungarian 'é' letter seems to be 'é', this is two utf8 character from this raw data: \xC3\x83\xC2\xA1.

    Thanks in advance.


  • Lifetime Qt Champion



  • This not working. The string type is wchar_t, only storing format is uft8. Now I see the utf8 result, this is wrong.


  • Lifetime Qt Champion

    @Kutyus I don't understand. The result of WideCharToMultibyte() is LPSTR, i.e. 8 bit character, not wchar_t.

    You should probably show some code how you insert and retrieve the data from the DB to get better help.



  • I was wrong, the stored string is utf8, but in hungarian language, the 'é' is correct result in latin-1 codepage.
    My new problem is how to translate it?


  • Lifetime Qt Champion

    Where do you use latin-1 now? Thought you're using a QString. Please show some code...



  • I was wrong again, it is a double encoding problem. The 'é' is 'é' in utf8, and 'é' is \xc3\x83\xc2\xa9 in utf8 and this last 4 byte is in my database.
    After select it the

    label.setText((query.value(7).toString());
    

    line is decoding one time, that's why i get the 'é'. So I would like to decode it one more time, but how? I tried more options without success.



  • I get the correct string with this query:

    convert(cast(convert(col using latin1) as binary) using utf8)
    

    How can it be done in a qt?


  • Lifetime Qt Champion

    @Kutyus

    have you tried:

    label.setText(QString::fromUtf8(query.value(7).toByteArray()));

    ?



  • Yes, same wrong result.
    And I tried these too:

    label.setText(QString::fromUtf8(QByteArray::fromStdString(QString::fromUtf8(query.value(7).toByteArray()).toStdString())));
    

    and

    label.setText(QString::fromUtf8(QByteArray::fromStdString(QString::fromLatin1(query.value(7).toByteArray()).toStdString())));
    

    but same wrong result.


Log in to reply