In QT, Chinese characters are often used, whether it is a string or a path name. Fortunately, QT provides the qtextcodec class, which has a powerful encoding format conversion function and provides most common character encoding formats in the world.
In QT, the string using qstring adopts unicode encoding. in windows, we use GBK encoding (a Chinese encoding). in Linux, UTF-8 encoding is usually used.
1. Use Chinese Characters in Windows
Add the following in the main () function:
1 # include <qtextcodec> 2 3 qtextcodec: setcodecforlocale (qtextcodec: codecforname ("GBK"); // path name supports Chinese characters 4 qtextcodec: setcodecfortr (qtextcodec :: codecforname ("GBK"); // qstring supports Chinese 5 qtextcodec: setcodecforcstrings (qtextcodec: codecforname ("GBK"); // string supports Chinese
2. in Linux
Replace the above "GBK" with "UTF-8 ".
3. Other cases
Sometimes, even if you add a file above, garbled characters still occur when you use Chinese Characters in other files. (if you do not mention it, I have encountered such problems ...). At this time, there are other solutions
For example, if tr ("Chinese") of qstring type is displayed, the result of "Chinese" is garbled, and TR ("Chinese") is replaced
Qtextcodec: codecforname ("GBK")-> tounicode ("Chinese ")
In this way, Chinese characters are displayed normally. In fact, this is also a solution to prevent Chinese garbled characters, but in this case, every place with a qstring of Chinese characters must be modified in this way, which cannot be solved, who told me to add those three sentences or garbled characters.
4. digress
Of course, QT provides translation for different languages ,. However, the program we write is only used in China, so there is no need for such high-end features. It is written in Chinese in the program.