qt 5 9 0 中文字元顯示

2021-10-08 10:44:31 字數 643 閱讀 5110

qt中的中文顯示,經常會出現亂碼。qt4中的解決方法,大致如下所示:

qtextcodec::setcodecfortr(qtextcodec::codecforname("utf8"));

qtextcodec::setcodecforlocale(qtextcodec::codecforname("utf8"));

qtextcodec::setcodecforcstrings(qtextcodec::codecforname("utf8"));

而qt5的解決方法類似於以上的方式,但是由於qtextcodec::setcodecfortr()和qtextcodec::setcodecforcstring()這兩個函式在qt5中已經沒有了所以做如下更改:

qtextcodec *codec = qtextcodec::codecforname("utf-8");//或者"gbk",不分大小寫

qtextcodec::setcodecforlocale(codec);

按照上述方式可解決針對qt4.8和qt5.0的版本的中文字元的顯示問題。

易百納技術社群內容涵蓋機器視覺、音訊處理、機器連線、機器感知以及智慧型終端,是乙個多方向的專業性社群,致力於為更多工程師答疑解惑,創造機遇!

QT 中文字元亂碼

qstring qbytearray 後位元組碼與window下的不對。下面的中文字元 中國人民億 data是我正常後的gbk位元組,value是直接使用 qstring totoutf8 qstring tolocal8bit 之類的與期望不符的位元組碼。首先qstring 轉換成qbytearr...

Qt開發 Qt中的中文字元問題

之前查了關於處理中文字元的一些資料,說法不一,有些方法又沒有效果。只好最終自己嘗試了一種適合當前程式的方法。1 首先在主函式中重置程式的預設字元編碼 int main int argc,char argv 其中qtextcodec codecforlocale 是用來返回系統的預設編碼的,另外也可以...

Qt中關於中文字元編碼問題

qstring filepath filepath qfiledialog getopenfilename null,qstring 開啟戶型圖 qstring qstring 戶型 件 bmp jpg png 編譯器報如下錯誤 e qt projects huxing src mainwindow...