[SOLVED] - QString and encoding (UTF-8)



  • Hi,

    I'm creating an object that has multiple QString, I would like them to support UTF-8 character (for example Chinese, russian, etc.)

    I have a web service that returns some database field as utf-8 (xml file with utf-8 format)
    Here is a real example:
    http://maximumtrainer.com/phprestsql/?username=max3&password=123&lang=fr&getUser=1
    Check for <first_name> and <last_name> for example of UTF-8 varchar

    When I am parsing this xml in QT, the chinese and russian character show as ???? instead of the real value, I have tested with french character (éèâ, etc.) and those works. Any idea why the other one are not shown, is it the qDebug console that is not utf-8 ? is it QString that is not utf-8 ?

    The parsing is not the the problem, because if I set directly the QString value to chinese character, I cannot display it in the console, example :
    @user->first_name = "喜喜";
    qDebug() << "USER FIRST NAME? IS " << user->first_name;@

    Thanks if you can give me a hand


  • Moderators

    Hi,

    [quote]
    @user->first_name = "喜喜";@
    [/quote]You usually cannot put unicode strings in your source code directly -- your compiler might not understand it.

    You need to use "Unicode string literals":http://stackoverflow.com/questions/7636797/unicode-string-literals, which was introduced in C++11.

    [quote]
    @qDebug() << "USER FIRST NAME? IS " << user->first_name;@
    [/quote]I'm not 100% sure, but I think qDebug() uses your system's console. Windows' console cannot print Unicode.

    You can try writing to a QLabel or a QFile instead.



  • Hi, you need to make sure that your compiler support utf8 executable charset if you want to use raw Chinese string literals. Though this is not a problem for most compilers, but MSVC have very bad support for utf8 exec-charset.



  • I guess the problem is the console/Windows, like you said JKSH
    If I ouput the result in a QLabel, it is displayed correctly
    @ QString str = "喜喜";
    ui->label_user->setText(str);@

    I'll go code on Ubuntu now :)

    Thanks for your input guys,


Log in to reply
 

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