PDA

View Full Version : Display Japanese character in qt GUI



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

Ginsengelf
4th May 2020, 13:48
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

prabhatjha
4th May 2020, 14:29
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");
}
}

d_stranz
4th May 2020, 15:40
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.

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

d_stranz
4th May 2020, 17:34
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 (http://www.cplusplus.com/) and CppReference.com (https://en.cppreference.com/w/) web sites are good places to look.