Why the Japanese sometimes garbled in utf8 mode?



  • I hava include this in main.cpp:

    QTextCodec::setCodecForLocale(QTextCodec::codecForName("utf8"));
    QTextCodec::setCodecForTr(QTextCodec::codecForName("utf8"));
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("utf8"));
    
    But the Japanese sometimes garbled.
    SO I am very confused about it。
    

    This is the picture。!http://static.oschina.net/uploads/space/2013/0514/164001_Aj8d_264056.png()!



  • Hi I had the same problems in an application I am working on and I had trouble displaying chinese characters in a QMessageBox for example.This got the problem solved:

    @ QString str;
    str=str.fromLocal8Bit("这是一条语句");
    mMessageBox.setText(str);@

    Hope this helps



  • Where does the text come from? You should make sure that the file it is in is utf8.
    I suggest using an external document you load and not text you type in your .cpp files.


  • Moderators

    musimbate: I would recommend making sure the code files are set to UTF-8 encoding and then use fromUtf8. fromLocal8Bit might work for you, but it will fail for your users that have a different default system encoding than the one you used to save your files.

    FromLocal8Bit is meant for text you got from the system (e.g. output of commands that were run), not for text where you can actually know the original encoding.



  • Thanks Tobias,
    My code files are set to UTF-8 but I was getting scrambled nonsense text displayed in my messageBox until I used FromLocal8Bit.Can my set up here cause any problems?
    [quote author="Thomas Zander" date="1368597121"]
    I suggest using an external document you load and not text you type in your .cpp files.[/quote]
    What if your application uses text that is directly embedded in your code.Want to know if there is something wrong with what I have done.
    Thanks.


Log in to reply
 

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