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;