Qt编码⽅式(中⽂显⽰)
最近在Qt上遇到界⾯显⽰中⽂乱码的问题,实现⽅法如下:
⽰例:
QUdpSocket *mUdpRcver;
QString    rcvData;
while (mUdpRcver->hasPendingDatagrams())
{
QByteArray datagram;
mUdpRcver->readDatagram(datagram.data(), datagram.size());
QTextCodec *tc = QTextCodec::codecForName("GBK");
rcvData = tc->toUnicode(datagram.data());
qDebug()<<"Rcv data is :"<<rcvData<<endl;
emit statusChanged();
}
将UDP发送的数据通过“GBK”编码⽅式转化成QString。
QString info = mBootReadCtrStatus-&Utf8();
mScanCtrStatus->setText(info);
显⽰窗⼝接收到UDP数据后通过UTF-8转化显⽰出中⽂。
详细说明Qt编码的⽅式:
1.Qt Creator -> ⼯具 -> 选项 -> 环境 - >概要 -> 语⾔
Qt Creator本⾝界⾯的语⾔选择,与cpp⽂件编码⽆关,与可执⾏⽂件显⽰编码⽆关
unicode汉字
重新打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: Utf8();
// 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", "\346\210\221\344\273\254", 0, QApplication::UnicodeUTF8));或
label->setText(QString::fromUtf8("\346\210\221\344\273\254"));
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")

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。