QTextCodec * BianMa = QTextCodec::codecForName ( "GBK" );
QMessageBox::information(this, "提示", BianMa->toUnicode("中文顯示!"));
其實也可以通過QString定義的靜態函數,先轉換成Unicode類型:
QString::fromLocal8Bit("提示")
不過在Qt5中,提供了一個專門的處理宏,來支持中文常量,那就是QStringLiteral,但它只能處理常量。
QMessageBox::information(this, QString::fromLocal8Bit("提示"), QStringLiteral("中文顯示"));
const char* info = "中文顯示";//不支持
QString strInfo = QStringLiteral(info);//支持
QString strInfo = QString::fromLocal8Bit(info);
----------------------------
VS2015+QT5.7.1
有一天上面幾種辦法都不能正常顯示中文了,直到找到下面這個解決辦法。
# pragma execution_character_set("utf-8")
____________________________
我在a.cpp使用b.cpp的接口,向b傳送一串中文字符串顯示時,上面所以方法都顯示亂碼。
需要按上圖把a.cpp和b.cpp文件都由UTF-8格式改為UTF-8無BOM格式就行,不需要上面的設置。