[Qt]Qt中文乱码问题

Ervin posted @ 2010年8月11日 05:45 in Qt with tags Qt , 6481 阅读

    在windows下编写Qt程序可能会遇到中文乱码问题。原因在于Qt内部使用Unicode编码,而windows的系统默认编码是GB18030/GBK/GB2312系列的。因此解决方法就是让Qt库进行正确的编码转换。

    在main中使用如下代码:

QTextCodec::setCodecForCStrings(QTextCodec::codecForName("utf8"));

“utf8”要跟文件编码一直。如果源文件采用GB2312编码,那么这里的utf8就应该改成GB2312。加入如上代码后就可以在源文件中直接使用中文了。

    如果使用了Qt的翻译机制,那么还应加入如下代码:

QTextCodec::setCodecForTr(QTextCodec::codecForName("utf8"));

PS:默认情况下Qt Creator使用系统编码,最好修改为utf8。

    更改方式:Tools->Options->Environment->Default file encoding 为 UTF8,然后新建的项目就会全部改为UTF8编码。

 

Avatar_small
自由民 说:
2011年7月29日 18:29

多谢博主!我在ubuntu上也碰到了中文乱码,照您的办法解决了!

Avatar_small
Sweetdumplings 说:
2012年7月12日 04:56

在Arch Linux上也遇到同样问题,这个方法的确是个方便的方法。

Avatar_small
전설 서구 说:
2021年2月27日 20:15

I have seen some great stuff here. Worth bookmarking for revisiting. I surprise how much effort you put to create such a great informative website. Your work is truly appreciated around the clock and the globe. gogo anime

Avatar_small
Bihar Board 10th Mat 说:
2022年9月27日 07:37

Mathematics is a very important subject to all medium students of BSEB, and the Patna board officials have published the Bihar Board 10th Mathematics Model Set 2023 question bank for English Medium, Bihar Board 10th Maths Model Paper Hindi Medium, Urdu Medium students with important questions at their Bihar Board Online website, and the BSEB 10th Maths Model Paper 2023 Pdf download available for all chapters along solutions suggested by BSEB experts.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter