From: http://www.sudu.cn/info/article/articleInfo.php? Aid = 286993.
Since QT returns unicode encoding, for example, if you directly enter Chinese Characters in lineedit, the returned result is ??, In QT, qtextcodec can be directly used to convert string encoding.
Qstring string;
String = lineedit1-> text (); // get the text returned by lineedit1
Qtextcodec * codec = qtextcodec: codecforname ("GBK ");
// Convert the Encoding
Qcstring
Chinese_string = codec-> fromunicode (string); // use qcstring to store the returned multi-byte encoding
Of course, you must add
# Include
# Include
# Include
4. qtextstream's support for Chinese
Qtextcodec * codec = qtextcodec: codecforname ("GBK");/* The current encoding is "GBK "*/
Qtextstream mystream (& file );
Mystream. setcodec (codec );
5. qcstring & qstring
Qcstring is not unicode encoded.
Qstring is encoded in Unicode.
Qt uses Unicode for encoding. To display Chinese Characters in QT components such as multilineedit, convert non-Unicode characters to unicode characters.
Qcstring locallyencoded = "Chinese are not patients in East Asia"; // text to convert
Qtextcodec * codec = qtextcodec: codecforname ("GBK"); // get the codec for GBK
Qstring unicodestring = codec-> tounicode (locallyencoded );
Qt international programming
This article discusses the QT library's support for internationalization
This section describes qt's support for text display, input, and printing, and how to use QT to develop internationalized software.
Qt's current version (2.2.4) provides excellent support for internationalization. In text display, QT uses Unicode as internal encoding and supports multiple types of encoding at the same time. It is also convenient to add a encoding support for Qt. You only need to add the encoding and Unicode conversion encoding. Qt currently supports ISO 8859-1, ISO 8859-2, ISO 8859-3, ISO 8859-4, ISO 8859-5, ISO 8859-7, ISO 8859-9, and ISO 8859-15 (support for Arabic and Hebrew is under development), Chinese GBK/big5, Japanese eucjp/JIS/shiftjis, Korean euckr, Russian KOI8-R. Of course, utf8 encoding can also be used directly.
Qt uses its own locale mechanism, which makes up for the shortcomings of locale and gettext widely used in UNIX in terms of encoding support and message file translation. This mechanism of QT enables the same component (qwidget) of QT to display different encoding texts at the same time. For example, QT labels can use both Simplified Chinese text and Traditional Chinese text.
In text input, QT adopts the xim (X Input Method) standard protocol and can directly use the xim input server. Currently, most input servers are for a single language, so the input in the standard QT input component (qlineedit, qmultilineedit) is subject to a single encoding, qt does not support dynamic conversion of encoding input.
1. text display of QT
Like the normal internationalization process, QT uses the same function qobject: TR () similar to GNU gettext, which is used to obtain information files from QT. information is extracted from Qm, which is processed by QT tools. Qt also uses the qtranslator class for coding and can be used to specify information files of the entire application software.
Use QT to write international programs. It is best not to directly use specially encoded text in the program. For example, to compile the QT program on the UI, you should use English in the program. After the program is compiled, extract the text and translate it. In this way, the program supports multiple languages based on locale. The following describes how to mark strings in QT programs and how to extract and translate texts.
The following is a piece of code using qobject: TR (). It creates a pop-up menu with the menu item "quit", which is placed on the menu bar, the label "file" is displayed on the menu bar ".
Qpopupmenu * popup;
Popup = new qpopupmenu (this );
Popup-> insertitem (TR ("& quit"), qapp, slot (quit ()));
Menubar-> insertitem (TR ("& file"), popup );
In most cases, you can use the preceding method. However, the preceding method cannot be used to define character strings used in some variables. However, to allow QT to extract and translate the string, some methods must be used to mark the strings. Qt defines qt_tr_noop () and qt_translate_noop () to mark them. The former is used for a single character string, and the latter is used for multiple strings. For example,
Static const char * strings [] = {
Qt_tr_noop ("hello "),
Qt_tr_noop ("world ")
};
Sometimes you need to use functions such as printf/sprintf to dynamically generate strings, for example,
Qstings S;
S. sprintf ("button % d", I );
But-> settext (s );
The ARG () function is used for internationalization.
Qstring S = tr ("button % 1"). Arg (I );
But-> settext (s );
The findtr Command provided by QT is used to extract the above information:
Findtr [filename]. cpp> i18n. Po
Similar to GNU xgettext, the extracted information file of the preceding file contains,
....
"Content-Type: text/plain; charset = iso-8859-1 \ n"
#: I18n. cpp: 34
Msgid "examplewidget: & file"
Msgstr ""
...
The next step is the text translation process. Attention should be paid to the following when translating information files in QT: (1) the encoding of the extracted information file is a iso-8859-1, and when translating it into a language (encoding), attention should be paid to changing its character set, for example, the encoding for Chinese gb2312 and big5 should be "Content-Type: text/plain; charset = gb2312 \ n" or "Content-Type: text/plain; charset = big5 \ n ". (2) The extracted information has a range. For example, if the range specified by the above file is examplewidget, remove it before translation and change it to msgid ": & File ". (3) The translated string may contain the accelerator key, for example, "F" in "& File". If it is best to retain the information in Chinese, it can be translated into "files (& F )".
For translated files (for example, the above translation file is saved as i18n_gb.po), you must use the tool msg2qm provided by QT to convert it to a. QM file,
> Msg2qm i18n_gb.po i18n_gb.qm
It is similar to the GNU msgfmt command. The translated files can be directly called using the QT program.
Qtranslator * Translator = new qtranslator (0 );
Translator-> load ("i18n_gb.qm ",".");
Qapp-> installtranslator (translator );
In addition, QT provides the msgmerge-like tool mergetr, which is used to combine the Newly Extracted information files with the translated information files.
In QT, qtextcodec can also be used directly to convert string encoding, which brings convenience to the Development of pure Chinese software in QT. However, this method is not consistent with the international/localization habits,
Char * string = "Chinese and English mixed strings! "
Qtextcodec * gbk_codec = qtextcodec: codecbyname ("GBK ");
Qstring gbk_string = codec-> tounicode (string );
Qlabel * label = new qlabel (gbk_string );
If the program supports only one encoding, you can set the encoding of the entire application to GBK encoding and add tr (qobject: TR) before the string ),
Qapp-> setdefacodecodec (qtextcodec: codecforname ("GBK "));
Qlabel * label = new qlabel (TR ("Chinese label "));
If you want QT to obtain the character set based on the locale environment variables, you can use qstring: fromlocal8bit (STR ).
For examples in this section, see qt-i18n-example.tar.gz
2. text input of QT
In terms of input, qlineedit and qmultilineedit support xim. You can enter Chinese, Japanese, and Korean in combination with the corresponding input server. Currently, there are many xim-supported software, such as Chinese: chinput/xcin/rfinput/Q9, Japanese: kinput2/skkinput, and Korean: Ami/Hanim.
The default input style of the QT program is overthespot. It also supports offthespot and root. You can specify the input style on the command line when starting a program, for example,
./APP-inputstyle overthespot # default style, cursor follows
./APP-inputstyle offthespot
./APP-inputstyle Root
The Qt-2.2.0 patched with mizilinux supports the onthespot input style and uses it as the default input style. See
All widgets in QT can accept input as long as they have keyboard focus ). Therefore, to process the input of a special widget, you only need to intercept the keyboard input and obtain the string from the xim server. For overthesport style support, refresh the xim input server location.
3. QT Printing
In terms of printing, QT in XWindow generates PostScript and uses LPR for printing. It contains the qprinter class, which can easily support control of output pages. For Chinese printing, the output part of the postscript file must be corrected.
Topia Culture
I. Character Set Introduction
China has issued a variety of Chinese Information encoding standards, commonly used: GB2312-1980, gb12345, gb13000 (GBK) and the latest standard gb18030, of which gb13000 is the extension of gb2312, it is often GBK, and gb18030 is backward compatible with gb2312 and GBK. The Chinese character set used by Chinese window98 and Chinese Win2000 operating systems is gb2312.
The gb2312 font only covers the dual-byte part. The storage location index is obtained from the 8bit of each byte in encoding. For example, the index of a1a1-Encoded chinese characters in the font is 2121, not a1a1. The following are their encoding rules:
Single byte: 00 ~ 7f
Double Byte: A1 ~ F7 A1 ~ Fe
The GBK encoding rules are as follows:
Single byte: 00 ~ 7f
Double Byte: 81 ~ Fe 40 ~ 7E
80 ~ Fe
Gb18030 is the latest Chinese character encoding standard. Its Encoding is 1, 2, and 4 variable-length encoding:
Single byte: 00 ~ 7f
Double Byte: 81 ~ Fe 40 ~ 7E
80 ~ Fe
Four bytes: 81 ~ Fe 30 ~ 39 81 ~ Fe 30 ~ 39
Unicode encoding adopts the same-length encoding, and two bytes represent one character encoding. ASCII codes are also expressed in two bytes. Unicode uses two-dimensional space to describe the encoding space, the plane is divided into 256 rows and 256 columns, which correspond to the encoded high/low bytes.
Ii. QT international programming
In text display, QT uses Unicode as the internal encoding. to internationalize the program, we usually do not directly enter local characters in the text display, instead of English, for example, to compile the QT program on the UI, you should use English in the program. After the program is compiled, extract the text and translate it. To translate a file, use the tr () function to identify the file in the text, generate a. QM file, and add qtranslator to the program. For example, we have the following statement in a program:
Setcaption (TR ("Main Window "));
To display Chinese characters, you can use either of the following methods:
Method 1:
L modify the project file and add translations = xxx. Ts
L lupdate project file name
L use linguist to edit the generated XXX. Ts file and save it
L lrelease project file name xxx. QM
L add qfont font1 ("unifont" "target =" _ blank "> http://www.mizi.com/ko/kde/doc/onthespot/onthespot.htmlin main. cpp.
All widgets in QT can accept input as long as they have keyboard focus ). Therefore, to process the input of a special widget, you only need to intercept the keyboard input and obtain the string from the xim server. For overthesport style support, refresh the xim input server location.
3. QT Printing
In terms of printing, QT in XWindow generates PostScript and uses LPR for printing. It contains the qprinter class, which can easily support control of output pages. For Chinese printing, the output part of the postscript file must be corrected.
Topia Culture
I. Character Set Introduction
China has issued a variety of Chinese Information encoding standards, commonly used: GB2312-1980, gb12345, gb13000 (GBK) and the latest standard gb18030, of which gb13000 is the extension of gb2312, it is often GBK, and gb18030 is backward compatible with gb2312 and GBK. The Chinese character set used by Chinese window98 and Chinese Win2000 operating systems is gb2312.
The gb2312 font only covers the dual-byte part. The storage location index is obtained from the 8bit of each byte in encoding. For example, the index of a1a1-Encoded chinese characters in the font is 2121, not a1a1. The following are their encoding rules:
Single byte: 00 ~ 7f
Double Byte: A1 ~ F7 A1 ~ Fe
The GBK encoding rules are as follows:
Single byte: 00 ~ 7f
Double Byte: 81 ~ Fe 40 ~ 7E
80 ~ Fe
Gb18030 is the latest Chinese character encoding standard. Its Encoding is 1, 2, and 4 variable-length encoding:
Single byte: 00 ~ 7f
Double Byte: 81 ~ Fe 40 ~ 7E
80 ~ Fe
Four bytes: 81 ~ Fe 30 ~ 39 81 ~ Fe 30 ~ 39
Unicode encoding adopts the same-length encoding, and two bytes represent one character encoding. ASCII codes are also expressed in two bytes. Unicode uses two-dimensional space to describe the encoding space, the plane is divided into 256 rows and 256 columns, which correspond to the encoded high/low bytes.
Ii. QT international programming
In text display, QT uses Unicode as the internal encoding. to internationalize the program, we usually do not directly enter local characters in the text display, instead of English, for example, to compile the QT program on the UI, you should use English in the program. After the program is compiled, extract the text and translate it. To translate a file, use the tr () function to identify the file in the text, generate a. QM file, and add qtranslator to the program. For example, we have the following statement in a program:
Setcaption (TR ("Main Window "));
To display Chinese characters, you can use either of the following methods:
Method 1:
L modify the project file and add translations = xxx. Ts
L lupdate project file name
L use linguist to edit the generated XXX. Ts file and save it
L lrelease project file name xxx. QM
L add qfont font1 ("unifont" to main. cpp"
, 16, 50, false, qfont: Unicode );
Qapp-> setfont (font1 );
Qtranslator * Translator = new qtranslator (0 );
Translator-> load ("XXX. QM ",".");
Qapp-> installtranslator (translator );
Method 2:
L findtr file name (usually CPP file)> XXX. Po
L edit the Po file where charset needs to be changed from iso-8859-1 to gb2312 and then translate "Main Window" into "Main Window"
L msg2qm? Scope zh_cn.gb2312 XXX. Po XXX. Q