1.Qt Creator -> 工具 -> 选项 -> 环境 - >概要 -> 语言
Qt Creator本身界面的语言选择,与cpp文件编码无关,与可执行文件显示编码无关
重新打Qt Creator 此设置才能生效,这个唯一选择<Chinese>
2.Qt Creator -> 工具 -> 选项 -> 文本编辑器 -> 默认文件编码
决定cpp文件编码,不同平台下此设置要相同。如果不同,cpp文件会有好多乱码,且不能被编辑,但不影响编译。重新打Qt Creator 此设置才能生效,这个推荐选<UFT-8>,因为linux下是这个。如果打开的文件不是UFT-8:Qt Creator -> 编辑 -> 选择编码,选择<根据编码重新载入>这步选择文件的原始编码,再<根据编码保存>这步选择需要保存的目标编码
3.QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
只与这个相关QString str = tr("中文")。注意,相关的是转换过程,QString本身是unicode,不会影响到QString,这个必须和Qt Creator -> 工具 -> 选项 -> 文本编辑器 -> 默认文件编码相同。推荐选<UFT-8>
4.QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
只与这个相关QString str = QString("中文")。注意,相关的是转换过程,QString本身是unicode,不会影响到QString,这个必须和Qt Creator -> 工具 -> 选项 -> 文本编辑器 -> 默认文件编码相同。推荐选<UFT-8>
5.QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
只与这个相关QString str =
QString::fromLocal8Bit("中文")。
以上2个最好都不用,用以下的
// 常规做法举例之GB2312 ,注意"UTF-16"的输入是要带BOM的
QTextCodec *pCodec = QTextCodec::codecForName("GB2312");//
"UTF-8", "UTF-16"
GB2312转QString: pCodec->toUnicode();
QString转GB2312: pCodec->fromUnicode();
// 另外QString类分别针对"UTF-8", "UTF-16"做了专门的函数, 当然结果同常规做法是一样的,例子如下:
// Unicode(utf8)(3个字节/汉字, 1个字节/字母)
UTF-8转QString: QString::fromUtf8();
QString转UTF-8: QString.toUtf8();
// Unicode(utf16) Unicode(ucs2) Unicode(iso-10646-ucs-2) 2个字节/字符,也是windows记事本里那个Unicode
// qt中表示UTF-16要用ushot,不要用wchar_t(wchar_t大小根据编译器而定)
UTF-16转QString: QString::fromUtf16(); //这个带2个字节的BOM头,[0xFF 0xFE](61,00) 或者[0xFE
0xFF](00,61)
QString转UTF-16: QString.utf16(); //返回以0结尾的ushot数组。
6.UI界面控件中可直接写对应中文内容,而不需要另外的转换
因为QString本身是unicode,保存到<ui_***.h>文件文件时是这样写的:
label->setText(QApplication::translate("Dialog",
"346210221344273254", 0, QApplication::UnicodeUTF8));或
label->setText(QString::fromUtf8("346210221344273254"));
7.文本文件的编码
windows平台文本文件有BOM
ANSI:没有文件头,2字节/汉字,1字节/英文
UTF-8:文件头[0xEF,0xBB,0xBF],3字节/汉字,1字节/英文
Unicode:文件头[0xFF,0xFE],2字节/汉字,2字节/英文
Unicode big endian :文件头[0xFE,0xFF],同Unicode,字节序相反
Linux平台UTF-8和GB2312文件都没有BOM
Linux平台UTF-16同windows一样,有BOM且相同
8.Qt支持的文本编码
("GBK", "CP936", "MS936", "windows-936",
"roman8", "hp-roman8", "csHPRoman8",
"TIS-620", "ISO 8859-11","WINSAMI2",
"WS2", "Apple Roman", "macintosh",
"MacRoman", "windows-1258", "CP1258",
"windows-1257","CP1257", "windows-1256",
"CP1256", "windows-1255", "CP1255",
"windows-1254", "CP1254",
"windows-1253","CP1253", "windows-1252",
"CP1252", "windows-1251", "CP1251",
"windows-1250", "CP1250", "IBM866",
"CP866","csIBM866", "IBM874", "CP874",
"IBM850", "CP850", "csPC850Multilingual",
"ISO-8859-16", "iso-ir-226",
"latin10","ISO-8859-14", "iso-ir-199",
"latin8", "iso-celtic", "ISO-8859-13",
"ISO-8859-10", "iso-ir-157", "latin6",
"ISO-8859-10:1992", "csISOLatin6", "ISO-8859-9",
"iso-ir-148", "latin5", "csISOLatin5",
"ISO-8859-8", "ISO 8859-8-I", "iso-ir-138",
"hebrew", "csISOLatinHebrew", "ISO-8859-7",
"ECMA-118", "greek", "iso-ir-126",
"csISOLatinGreek", "ISO-8859-6", "ISO-8859-6-I",
"ECMA-114", "ASMO-708", "arabic",
"iso-ir-127", "csISOLatinArabic",
"ISO-8859-5","cyrillic", "iso-ir-144",
"csISOLatinCyrillic", "ISO-8859-4", "latin4",
"iso-ir-110", "csISOLatin4", "ISO-8859-3",
"latin3","iso-ir-109", "csISOLatin3",
"ISO-8859-2", "latin2", "iso-ir-101",
"csISOLatin2", "KOI8-U", "KOI8-RU",
"KOI8-R", "csKOI8R", "UTF-8",
"ISO-8859-1", "latin1", "CP819",
"IBM819", "iso-ir-100", "csISOLatin1",
"ISO-8859-15", "latin9","UTF-32LE",
"UTF-32BE", "UTF-32", "UTF-16LE",
"UTF-16BE", "UTF-16", "System",
"Iscii-Mlm", "Iscii-Knd", "Iscii-Tlg", "Iscii-Tml",
"Iscii-Ori", "Iscii-Gjr", "Iscii-Pnj",
"Iscii-Bng", "Iscii-Dev", "TSCII",
"GB18030", "GB2312", "EUC-JP","ISO-2022-JP",
"Shift_JIS", "JIS7", "SJIS",
"MS_Kanji", "EUC-KR", "cp949", "Big5",
"Big5-HKSCS", "Big5-ETen","CP950")