Results 1 to 2 of 2

Thread: Display Japanese character in qt GUI

  1. #1
    Join Date
    Aug 2013
    Location
    India
    Posts
    44
    Thanks
    3
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Display Japanese character in qt GUI

    Hello Everyone,

    I have crated a GUI and setting the menu bar, tab names, dialog titles / notations, and right-click menus from a .csv file.
    Initially i have added English character in .csv file and everything displaying correct in GUI.
    but when i have inserted Japanese character in .csv file. in Menu Bar and all showing some garbage value.

    // refrence code
    QString str = GlobalScreenNotation::getInstance()->read_record(1).c_str(); //reading data from .csv file at position 1
    QByteArray byteArray = str.toUtf8();
    char* data = byteArray.data();
    setWindowTitle(data);

    Please assist me, how i can resolve this issue.

    Reagrds,
    Prabhat

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Display Japanese character in qt GUI

    Is there a reason you did not use the Qt internationalisation support (Qt Linguist and the TR() macros)?

    Qt Code:
    1. // refrence code
    2. QString str = GlobalScreenNotation::getInstance()->read_record(1).c_str(); //reading data from .csv file at position 1
    3. QByteArray byteArray = str.toUtf8();
    4. char* data = byteArray.data();
    5. setWindowTitle(data);
    To copy to clipboard, switch view to plain text mode 
    Line 2 (presumably) returns a char* to your title from the file. The const char pointer is converted using the QString::fromUtf8() function and stored in str. If the input is not UTF-8 encoded then your process is broken from this point. We cannot see your file or what your function returns. For example, the characters Screenshot_20200509_115643.png (U+984C U+540D) should result in the six bytes E9 A1 8C E5 90 8D in a UTF-8 file.

    Line 3 and 4, convert the QString back to UTF8 and get a pointer to a character buffer.

    Line 5 passes the character buffer to QWidget::setWindowTitle() which expected a QString in the first place. This causes another pass through QString::fromUtf8() to get a QString.

    So, you only really need line 2 and 5, and to understand exactly what encoding the C string coming out of read_record() is. My guess is that the file is not UTF8 encoded, possibly Windows UTF16 but it could a number of other things.

    Another possibility is that the font used to render the window title does not contain glyphs corresponding to the characters supplied.


    Edit: the Japanese characters appear correctly in the preview but fail when actually posted, so you have an image instead
    Edit 2: Oh, and please do not double post.
    Last edited by ChrisW67; 9th May 2020 at 04:06. Reason: updated contents

Similar Threads

  1. Display Japanese character in qt GUI
    By prabhatjha in forum Qt Programming
    Replies: 5
    Last Post: 4th May 2020, 18:34
  2. Japanese character read from html file and show in QTextEdit
    By santosh.kumar in forum Qt Programming
    Replies: 2
    Last Post: 30th December 2016, 11:25
  3. Replies: 5
    Last Post: 8th September 2011, 11:38
  4. Problem with chinese and japanese characters display
    By manojmka in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 26th February 2010, 06:09
  5. How to display individual character of a string?
    By cooper in forum Qt Programming
    Replies: 2
    Last Post: 15th July 2009, 06:19

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.