Qt5中文亂碼問題
- 1.問題現象
- 2.解決方法
-
- 2.1 有翻譯需求(使用tr)
- 2.2 不需要翻譯
- 3.編碼修改
-
- 3.1 Qt Creator設定編碼
- 3.2 Qt Creator右鍵add UTF-8 BOM無效
- 4.參考連結
1.問題現象
環境:win10 使用Qt5.12 + MSVC2017編譯
當tr(“奇數個漢子”)或者tr(“中文與—123其它字元一起顯示”)
Qt報錯:error C2001: newline in constant
添加UTF-8 BOM 之後可以編譯但是中文亂碼
2.解決方法
2.1 有翻譯需求(使用tr)
每一個包含中文字元的檔案頭部中添加
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif
或者tr中寫英文 後續再翻譯
2.2 不需要翻譯
使用QStringLiteral(“中文”);
3.編碼修改
3.1 Qt Creator設定編碼
建議在creator中修改預設編碼,友善
3.2 Qt Creator右鍵add UTF-8 BOM無效
使用别的編輯軟體(文本編輯器),如Notepad++,打開檔案修改編碼
4.參考連結
- https://blog.csdn.net/aiyaya333/article/details/107310775 – QT報錯解決:error C2001: newline in constant
- https://blog.csdn.net/seamanj/article/details/70165273 – 關于QT中奇數個漢字出現newline in constant的錯誤