Qt World Summit: Submit your Presentation

QLinguist and Chinese

  • Hi,

    I have an embedded system all working great and now need all static text to be shown in Chinese. I have found a chinese unicode ttf font and confirmed that this contains chinese characters above 255 and that they can be displayed in QTextEdit (thus, QPushButton, QLabel etc...). I have also confirmed that QLinguist along with the tr() function works great for utf8 translations. I have been trying to get this to work now with the Chinese ttf and no joy. I can get the chinese text into QLinguist by editing in Word and then pasting into the translation window but when I test the QM file in Qt I just get a blank string where the translation should be. I know the Qt controls can display the extended characters and I'm pretty sure the QM file contains the Chinese unicode - so the issue is maybe somewhere in the loading of the QM file?

    I thank anyone in advance if they can help and would be happy to provide more info if neccessary.

    Thank you.



  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Did you make your font available to your application using QFontDatabase ?

  • @SGaist Hi, thanks for reply.

    Yes I sent a QChar(1000) character into the QLabel and it displayed the same character as on the PC using the same Chinese font - so I'm fairly sure the Font is installed and working.

    Again, thank you.

  • Lifetime Qt Champion

    The fact that it works on your desktop machine doesn't mean that it will on your target device. Did you at least deploy the font on your device ?

  • @SGaist
    Hi, sorry there seems to be some misunderstanding.

    The font has been deployed both on the target device and the host (windows) development system.

    I have not written any Qt to run on the host - only on the embedded Linux target.

    So I have confirmed that Qt controls like QTextEdit and QLabel correctly display extended characters from the character set by correlating what is displayed appending QChar(0x1000) against what Word displays on the PC when I enter 1000<alt-x>.

    As I said, I think my issues are that the QM file is not gettting the correct unicode translation data or the QTranslator.load() command is somehow not loading the unicode from the QM file.

    Again, thank you.



  • @Gazzer before focusing on .qm file I'd like to be sure the translation text file (.ts) is correct. I'm working with Spanish accented characters, and my .ts file is UTF-8 encoded, I can see the "special characters" Ok on Qt Linguist, and if I change the encoding to ANSI, I can see the 2+ bytes used by such special chars.

    So, are you experiencing all this the same as me? I mean, are you able to see the proper Chinese characters while creating the translations with Qt Linguist? What if you open the .ts file with a text editor?. Only after confirming that this is OK I'll move into the compilation phase (getting .ts into .qm file) as the culprit.

    Just my 2 cents.

  • @Pablo-J.-Rogina Thanks for reply. Yes, I have confirmed that the chinese characters are indeed in the .TS file by viewing with XMLNotepad. Also, I can close QLinguist down, restart and reload the .TS file and still view the chinese characters in the translation.

    I then used the Release command in QLinguist and created a new .QM file. I copied this to my target file system and modified by target app to QTranslate.load() this new .QM file but still only saw blanks or nothing where the Chinese should be. I made one of the translations half Chinese and half English just to confirm the translation is working and the English half only displays correctly...

    If you have a few more cents in your pocket I would be most grateful ha!


  • @Gazzer it looks like my little piggy bank may not be empty yet... as lrelease is a likely suspect.

    Could you please check the following using an hex editor:

    1. Open the .ts file and look for any translation you made that have a Chinese character, taking at least 2 bytes. My example is a word containing accented o (ó) in Spanish, and that letter is encoded in UTF-8 as 0xC3B3.
    2. Open the .qm file (genereated using lrelease) and check the same word. In my case I just found it takes only 1 byte 0xF3) which is the rerepsentation for the same character ó in Latin1 and matches the Unicode entry point U+00F3.

    So the solution in your case seems to come from both a) adding CODECFORTR=UTF-8 to your .pro file, so lupdate and b) calling QTextCodec::setCodecForTr("UTF-8") from the application's main() function.

  • @Pablo-J.-Rogina Thanks for the extra raid on your piggy bank... After your last comment. I then stepped through the target code and observed that the unicode characters were correctly being translated into a QString "/40000/40001/40002/40003" but not displayed on a QLabel. I then reconfirmed that the character QChar(1000) does display (and also confirmed the QLabel had the correct font set!) and can now only conclude that there are gaps in the font or glyphs that cannot render on our device but can on windows.

    As a reminder. I found a chinese ttf font (HanWang KaiBold-Gb5.ttf). Installed this on windows to get the translation text into QLinguist and then loaded on the embedded target - hoping to get exactly the same characters for each unicode.

    Thank you.

  • @Gazzer I'm glad that you find a workaround. If that finally solves you issue, please don't forget to mark this post as SOLVED.
    Thank you.

  • @Pablo-J.-Rogina I only discovered - with your help - my real problem. lack of a good or complete Chinese font. I also discovered that a ttf font does not work the same on embedded system as it does on windows.

    Back to drawing board but thank you for your help...

  • I am having this exact problem. I compiled my project for an embedded arm device, linux desktop, and windows desktop. The windows and linux hosts display the characters correctly, where the embedded device just shows nothing.

    I'm using the Noto fonts https://www.google.com/get/noto/#sans-hans and am positive it is being used on the linux desktop, since if I don't copy the font into the same directory as the application, it just shows blocks. When the font is in the directory, it shows everything properly.

    I tried the same thing with the embedded build, and nothing shows up. The font being loaded, since if it's not in the directory, it complains that it can't load it. Is there some extra switch or rendering tool/application that embedded linux needs? I've tried with both 'linuxfb' and 'egl' platforms with no success.

    Thanks in advance for any help.