PDA

View Full Version : Display Japanese character in qt GUI



prabhatjha
4th May 2020, 12:15
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

ChrisW67
9th May 2020, 02:51
Is there a reason you did not use the Qt internationalisation support (Qt Linguist and the TR() macros)?



// 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);

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 13436 (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.