Qt 使用Unicode编码来存储操作字符串,但很多情况下,我们不得不处理采用其他编码格式的数据,举例来说,中文多采用GBK和Big5编码,而日本则多采用Shift-JIS or ISO2022编码。
将其他编码格式的字符串转化成采用Unicode编码的QString,然后传递给qml,直接显示即可
// 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;
转载于:https://blog.51cto.com/2161404/1825629