PDA

View Full Version : Manage japanese charanters inside QPlainText



Suppaman
9th December 2011, 09:04
Hi all

Looked in the forum for similar problem but the few arguments found not give me a reply unfortunately...

I want to develop a japanese editor using Qt, however I'm facing some problem around it. First of all it seem Qt Creator doesn't manage to save source code file in unicode format. This mean if I try to put into the code something like:

string = "わたし"

and close and reopen the project instead of the japanese characters above I find something like:

string = "???"

Anyway this is not a big problem. I can use the unicode character number instead of the direct typing. I can assign the unicode number of the character I'm interested in to a wchar_t variable and manage using this. However this second solution come with other problems. My japanese editor will be an open source project. I want to create some "indipendent" c++ class for manage some special operation like, for example, the conversion from romaji to hiragana/katakana or the reverse. With the "indipendent" word I mean I want to develop these classes "Qt free" for allow other people to get it and use then in their project that could be not Qt based but using a different architecture. For accomplish this goal inside the class I want to use the wstring for manage unicode text. Here I found the first problem that is not directly connected to Qt but if somehone can give me a suggestion about I'll be very graceful. ;)

How can I convert wchar_t into wstring? I have a vector of wchar_t characters code, how can I use these codes for "compose" a wstring text? I still didn't find a way to this conversion.

So this class, after executed the requested operation, whatever is, will return a wstring with the japanese text to show inside the edit control (mainly I prefer to use a QPlainTextEdit control but also QTextEdit could be good if the first miss some feature). For conversion of this wstring I think I can use the QString::fromUtf16() method, is it correct?.

Now the second problem. When I try to assign some japanese text to my textedit control it show always ??? characters instead of japanese text. I thought it could be a problem of the font used (which font I need to use for make my code portable between Windows, Linux and mac?) but if I try to write some japanese text directly into the edit control using the Windows IME I can correctly see the japanese text over it. Than the font is correct? I'm a bit confused regarding unicode problems... :o

Excuse me for the long post

Thank you

Oleg
9th December 2011, 13:49
Use QObject::trUtf8()


string = trUtf8("わたし");

or add to your .pro:


CODECFORTR = UTF-8

and use:


string = tr("わたし");

Suppaman
9th December 2011, 14:02
Hi Oleg

Thank you for your reply but I don't understand to which of the problems I listed you are referring.

As written I can not use japanese characters inside the source code file since Qt Creator seem to doesn't save the source file in unocode format (close and reaopen the project will show only ??? characters instead of japanese text). Additing this option the the .pro file fix this issue?

Thank you

Spitfire
9th December 2011, 15:14
Which version of QtCreator are you using?

This (https://bugreports.qt.nokia.com//browse/QTCREATORBUG-1857)may help for your problems with saving files.:

You can change the default file encoding in Tools -> Options -> Environment -> General.

Suppaman
9th December 2011, 15:38
Hi

I have the latest version 2.3.1 on Qt Creator. I already noted the option settings for set the file encoding manged by Qt Creator but the only doubt I have is that this is a setting of the local installation of Qt Creator. This mean if another programmer will open my project using his Qt Creator that could not have this settings for unicode he'll "loose" al the japanese characters inside the code in the same time he'll try to save some modify.

Anyway this is a problem I can fix with some work. Currently my main priority is to manage japanese text in the editor control. Should I set some particular font for have it working well?

Thank you to all for the help

Oleg
9th December 2011, 16:29
Oops, not completely read original post, sorry.

It depends upon OS, for example QtCreator on my Ubuntu with Russian locale has no problems with saving and reading Japanese symbols with default settings and fonts. I think the same behavior will be on any UTF-8 system.
In other cases setting default file encoding should fix all your issues.

Suppaman
9th December 2011, 16:50
Hi

Don't worry, I thank you for the time you are dedicating me.

Reading my last post I know I didn't explain very well I was talking about. I develop a Qt application that should work as japanese editor. Than I set as central widget a QPlainTextEdit control but when I try to insert japanese text inside the editor show only "???" instead japanese characters. Than I asked if the problem could be in the font set to used by the QPlainTextEdit control....

Thank you

d_stranz
10th December 2011, 17:00
How can I convert wchar_t into wstring? I have a vector of wchar_t characters code, how can I use these codes for "compose" a wstring text? I still didn't find a way to this conversion.


I am confused by your question. The std::wstring constructor and operator= both will accept wchar_t * arguments, so you can simply assign it:



wchar_t * myWChar_tString;
std::wstring myWString = myWChar_tString;


But I am sure you know this. Maybe you can give us an example of one of your "vector of wchar_t characters code"?

Suppaman
13th December 2011, 08:02
Hi

Thank you for your help

I fixed the problem of wchat_t to wstring, now I need to look for a solution for show japanese text inside my QPlainTextEdit control.

I'll make an example:

myEditControl->setPlainText("わたし");

doesn't show inside the text edit control the japanese characters but show characters "???" instead. I suspect I need to set a correct font that include japanese characters set but what is the font I need to use for ensure a compatibility from Windows, Linux and Mac?

Thank you

Spitfire
13th December 2011, 09:51
As you said previously that when you save and open file in qt creator japanese symbols are replaced with "?".
I guess that's exacly what you see here.

When compiler works on the files, all there is is a bunch of "???" so that's what you see in the control.

Can you actually enter japanese symbols into the text edit using a keyboard while the app is running?
For me it works (copy/paste into the text edit in out-of-a-box qt app) but doesn't when seting it programatically.

Suppaman
13th December 2011, 20:22
Hi

Yes, it's exactly the same situation of mine. I can correctly insert japanese symbols using keyboard but no from inside the code. This mean when the Qt Creator start the compilation process save the file just before and during saving the japanese characters are lost, I understand right your idea? If so I need a way to convert japanese unicode code to insert them into the edit control. I'll making some other experiment following this way.

Thank you for the suggestion

EDIT: and this was really the problem. The following code work as expected to insert a japanese character into the text edit using the unicode code:

wchar_t code[2] = {0x3042, 0};
wstring text(code);
TextControl->setPlainText(QString::fromStdWString(text));

Thank you to all for the help ;)