Results 1 to 6 of 6

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
    Jul 2008
    Location
    Germany
    Posts
    503
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Display Japanese character in qt GUI

    Hi, GlobalScreenNotation::getInstance()->read_record(1).c_str() looks like it creates an ASCII string, not Unicode. Your Japanese character probably gets converted to multiple one-byte characters, which is the garbage you see.

    Ginsengelf

  3. The following user says thank you to Ginsengelf for this useful post:

    prabhatjha (4th May 2020)

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

    Default Re: Display Japanese character in qt GUI

    Hi Ginsengelf,

    Thank you so much for your response.

    GlobalScreenNotation::getInstance()->read_record(1) return string. could you please let me know how i can solve this issue.

    // code for read .csv file
    string GlobalScreenNotation::read_record(float no)
    {
    std::fstream fin;
    fin.open("screen_notation.csv", ios::in);

    try
    {
    if (!fin)
    {
    throw ".csv file is not presrent";
    }
    int flag = 0;
    char c = '"';
    vector<string> row;
    string line, word, temp;
    int count = 0;
    while (fin)
    {
    row.clear();
    getline(fin, line);
    if (line.empty())
    continue;
    stringstream s(line);
    count = 0;
    while (getline(s, word, ';'))
    {
    row.push_back(word);
    count++;
    }
    if (row[0][0] >= '0' && row[0][0] <= '9')
    {
    this->idNotation = stof(row[0]);

    if (this->idNotation == no)
    {
    if (count == 1)
    throw "Data is NULL";
    return trim(row[1]);
    }
    }
    }
    throw "Data is NULL";

    }
    catch (...)
    {
    cout << "issue in csv file reading";
    fin.close();
    QMessageBox msgBox;
    msgBox.setText("no record found");
    msgBox.exec();
    system("taskkill /F /T /IM PointCloudCleaning.exe");
    }
    }

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Display Japanese character in qt GUI

    You need to convert your code to use std:: wstring and wchar_t everywhere. std:: string and char do not support unicode. This conversion includes using the correct type of stream (std:: wfstream) to read your file.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. The following user says thank you to d_stranz for this useful post:

    prabhatjha (4th May 2020)

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

    Default Re: Display Japanese character in qt GUI

    Hi @Guru,

    Thank you so much for your kind support.

    could you please assist me as i am not aware what should i change for std:: wstring and wchar_t.

    * I have to read text from .csv file ( that is Japanse character "3D????????????")
    I am reading the .csv file from this code "GlobalScreenNotation::getInstance()->read_record(1)" .
    return value of "GlobalScreenNotation::getInstance()->read_record(1)" is std::string .

    For displaying the Japanese character code :

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

    Could you please let me know in shared code, what i should change?

    Thanks in advance.

    Regards,
    Prabhat

  8. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Display Japanese character in qt GUI

    Could you please let me know in shared code, what i should change?
    I told you in the previous answer what has to be changed and how. Any place that now uses string must be changed to wstring, any place using char has to use wchar_t. Any class (GlobalScreenNotation) that reads ASCII (as string) has to be changed to read unicode (as wstring). Any stream / fstream objects must be changed to wstream / wfstream.

    If you read something from a file as std:: string, there is no good way to convert it to wstring without producing garbage. You have to read it from the file as unicode if that's what the file contains. Even if the file contains standard ASCII, you can still read it as unicode and get the correct result, but not the other way around.

    I am not going to write your code for you. You should get into the habit of writing new code to support unicode from the beginning. Never use char, use wchar_t, never use std:: string, use std:: wstring, use the unicode version of stream and string functions. If you don't know the unicode (wide character) versions of these functions, the CPlusPlus.com and CppReference.com web sites are good places to look.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. 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
  2. Replies: 5
    Last Post: 8th September 2011, 11:38
  3. 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
  4. How to display individual character of a string?
    By cooper in forum Qt Programming
    Replies: 2
    Last Post: 15th July 2009, 06:19
  5. Determine if a font can display a character
    By jrideout in forum Qt Programming
    Replies: 1
    Last Post: 19th October 2006, 11:33

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.