Qt出現亂碼解決辦法

2021-08-10 22:43:59 字數 1233 閱讀 7636

qt中檔案路徑中出現漢字時會顯示亂碼,可在main函式中增加以下語句:

qtextcodec* textcodec=qtextcodec::codecforname(「gb2312」);

qtextcodec::setcodecforlocale(textcodec);

qtextcodec::setcodecforcstrings(textcodec);

qtextcodec::setcodecfortr(textcodec);

或qtextcodec *codec = qtextcodec::codecforname(「gbk」);

qtextcodec::setcodecforlocale(codec);

qtextcodec::setcodecforcstrings(codec);

qtextcodec::setcodecfortr(codec);

在main函式中設定

//設定中文編碼

#if qt_version < qt_version_check(5,0,0)

qtextcodec::setcodecforlocale(qtextcodec::codecforname(「gb2312」));

#else

qtextcodec::setcodecforlocale(qtextcodec::codecforname(「utf-8」));

#endif

在需要的標頭檔案中加入

qt5中在有漢字的cpp檔案中新增#pragma execution_character_set(「utf-8」)

如果你的**是從別的地方貼上過來的可能不是utf-8格式,可以使用notepad++轉換一下格式再把**貼上回去。

試試qt提供的巨集qstringliteral(),僅支援qt5

以上都不行的話試試下面的**:

qstring filepath;

qtextcodec codec = qtextcodec::codecforname(「gbk」);

qtextcodec::setcodecforlocale(codec);

qbytearray ba = filepath.tolocal8bit();

char tempchar = ba.data();

string tempfile = tempchar;

檢視你的**是用qt creator建立的還是vs直接建立的,建立後從qt creator轉到vs上或者vs轉到qt creator上。

HTML中文出現亂碼的解決辦法

meta charset utf 8 這是最簡便的方式。值得注意的是,如果你的 是在記事本裡面編輯的,那儲存時注意選擇編碼方式 utf 8.如果沒用,繼續向下看 charset gb2312 我覺得這算是一種不太嚴謹的解決方式。具體原理大家可以自行學習漢字編譯碼的有關知識。尤其是gb2312和utf...

mysql重灌後出現亂碼解決辦法

檢視當前連線系統引數 show variables like char mysql show variables like char variable name value character set client gbk character set connection gbk character...

FileItem 出現部分中文亂碼解決辦法

首先要進行兩處的修改 第一 如果你使用了上傳檔案的包,如 servletfileupload sfu new servletfileupload factory sfu.setheaderencoding iso8859 1 或者diskfileupload fu new diskfileuploa...