Qt 使用Unicode编码来存储操作字符串,但很多情况下,我们不得不处理采用其他编码格式的数据,举例来说,中文多采用GBK和Big5编码,而日本则多采用Shift-JIS or ISO2022编码。本文将讨论如何将其他编码格式的字符串转化成采用Unicode编码的QString
// Method 1
QString str = QString::fro mLocal8Bit("本地文本");
QString str2 = QString("本地文本"); // 乱码
// Method 2
QTextCodec *codec = QTextCodec::codecForName("GBK"); // get the codec for KOI8-R
QString locallyEncoded = codec->toUnicode( "显示中文" );
qDebug() << locallyEncoded << endl;
//更多细节请参见:
http://www.kuqin.com/qtdocument/qtextcodec.html
http://blog.youkuaiyun.com/catamout/article/details/5675878