1 using namespace std; 2 3 //#pragma execution_character_set("UTF-8") 4 int main(int argc, char *argv[]) 5 { 6 QApplication a(argc, argv); 7 QString str = QStringLiteral("中文测试"); 8 //str = "中文测试"; 9 qDebug() << str; 10 //QMessageBox::information(0, "title", str); 11 char *src = "元数据中文GBK"; 12 13 //元数据是gbk或者gb2312 多字节存入QString 14 //本地编码方式 默认GBK 15 QString str1 = QString::fromLocal8Bit(src); 16 qDebug() << "str1 = " << str1; 17 18 //把QString 转为gbk 19 cout << str1.toLocal8Bit().toStdString() << endl; 20 //本地处理编码方式,默认是GBK,改为UTF-8 21 QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8")); 22 QString str2 = QString::fromLocal8Bit(str1.toUtf8()); 23 24 QTextCodec::setCodecForLocale(QTextCodec::codecForName("GBK")); 25 qDebug() << "str2 = " << str2; 26 //win api调用qstring作为参数 27 MessageBox(0, str2.toStdWString().c_str(), L"中文标题",0); 28 29 //vs_cn_codec w; 30 //w.show(); 31 return a.exec(); 32 }
来源:https://www.cnblogs.com/linxuemufeng/p/12432526.html