qt 編碼設定
1.qt creator -> 工具 -> 選項 -> 環境 -> 語言
qt creator本身介面的語言選擇
與cpp檔案編碼無關,與可執行檔案顯示編碼無關
重新打qt creator 此設定才能生效
這個唯一選擇
2.qt creator -> 工具 -> 選項 -> 環境 -> 預設檔案編碼
決定cpp檔案編碼
不同平台下,此設定要相同,如果不同,cpp檔案會有好多亂碼,且不能被編輯,但不影響編譯;
重新打qt creator 此設定才能生效
這個推薦選,因為linux下是這個;
如果不同:
選擇《根據編碼重新載入》這步選擇檔案的原始編碼
再《根據編碼儲存》這步選擇目標編碼
3.qtextcodec::setcodecfortr(qtextcodec::codecforname("utf-8"));
只與這個相關qstring str = tr("中文");
注意,相關的是轉換過程,qstring本身是unicode,不會影響到qstring
這個必須和<2. qt creator -> 工具 -> 選項 -> 環境 -> 預設檔案編碼》相同
推薦選4.
qtextcodec::setcodecforcstrings(qtextcodec::codecforname("utf-8"));
只與這個相關qstring str = qstring("中文");
注意,相關的是轉換過程,qstring本身是unicode,不會影響到qstring
這個必須和<2. qt creator -> 工具 -> 選項 -> 環境 -> 預設檔案編碼》相同
推薦選5.qtextcodec::setcodecforlocale(qtextcodec::codecforname("utf-8"));
只與這個相關qstring str = qstring::fromlocal8bit("中文");
以上2個最好都不用,用以下的
// 常規做法舉例之gb2312 ,注意"utf-16"的輸入是要帶bom的
qtextcodec *pcodec = qtextcodec::codecforname("gb2312");// "utf-8", "utf-16"
gb2312轉qstring: pcodec->tounicode();
qstring轉gb2312: pcodec->fromunicode();
// 另外qstring類分別針對"utf-8", "utf-16"做了專門的函式, 當然結果同常規做法是一樣的,例子如下:
// unicode(utf8)(3個位元組/漢字, 1個位元組/字母)
utf-8轉qstring: qstring::fromutf8();
qstring轉utf-8: qstring.toutf8();
// unicode(utf16) unicode(ucs2) unicode(iso-10646-ucs-2) 2個位元組/字元,也是windows記事本裡那個unicode
// qt中表示utf-16要用ushot,不要用wchar_t(wchar_t大小根據編譯器而定)
utf-16轉qstring: qstring::fromutf16(); 這個帶2個位元組的bom頭; [0xff 0xfe](61,00) [0xfe 0xff](00,61)
qstring轉utf-16: qstring.utf16(); 返回以0結尾的ushot陣列
7.文字檔案的編碼
windows平台文字檔案有bom
ansi:沒有檔案頭,2位元組/漢字,1位元組/英文
utf-8:檔案頭[0xef,0xbb,0xbf],3位元組/漢字,1位元組/英文
unicode:檔案頭[0xff,0xfe],2位元組/漢字,2位元組/英文
unicode big endian :檔案頭[0xfe,0xff],同unicode,位元組序相反
linux平台utf-8和gb2312檔案都沒有bom
linux平台utf-16同windows一樣,有bom且相同
8.("gbk", "cp936", "ms936", "windows-936", "roman8", "hp-roman8", "cshproman8", "tis-620", "iso 8859-11",
"cp1257", "windows-1256", "cp1256", "windows-1255", "cp1255", "windows-1254", "cp1254", "windows-1253",
"cp1253", "windows-1252", "cp1252", "windows-1251", "cp1251", "windows-1250", "cp1250", "ibm866", "cp866",
"csibm866", "ibm874", "cp874", "ibm850", "cp850", "cspc850multilingual", "iso-8859-16", "iso-ir-226", "latin10",
"iso-8859-14", "iso-ir-199", "latin8", "iso-celtic", "iso-8859-13", "iso-8859-10", "iso-ir-157", "latin6", "iso-8859-
10:1992", "csisolatin6", "iso-8859-9", "iso-ir-148", "latin5", "csisolatin5", "iso-8859-8", "iso 8859-8-i", "iso-ir-
138", "hebrew", "csisolatinhebrew", "iso-8859-7", "ecma-118", "greek", "iso-ir-126", "csisolatingreek", "iso-
8859-6", "iso-8859-6-i", "ecma-114", "asmo-708", "arabic", "iso-ir-127", "csisolatinarabic", "iso-8859-5",
"cyrillic", "iso-ir-144", "csisolatincyrillic", "iso-8859-4", "latin4", "iso-ir-110", "csisolatin4", "iso-8859-3", "latin3",
"iso-ir-109", "csisolatin3", "iso-8859-2", "latin2", "iso-ir-101", "csisolatin2", "koi8-u", "koi8-ru", "koi8-r",
"cskoi8r", "utf-8", "iso-8859-1", "latin1", "cp819", "ibm819", "iso-ir-100", "csisolatin1", "iso-8859-15", "latin9",
"utf-32le", "utf-32be", "utf-32", "utf-16le", "utf-16be", "utf-16", "system", "iscii-mlm", "iscii-knd", "iscii-
tlg", "iscii-tml", "iscii-ori", "iscii-gjr", "iscii-pnj", "iscii-bng", "iscii-dev", "tscii", "gb18030", "gb2312", "euc-jp",
"iso-2022-jp", "shift_jis", "jis7", "sjis", "ms_kanji", "euc-kr", "cp949", "big5", "big5-hkscs", "big5-eten",
"cp950")
QT編碼設定問題
通常在windows環境下,對於中文編碼,需要在main.cpp中加上以下幾行 qtextcodec gbk qtextcodec codecforname gb18030 qtextcodec setcodecfortr gbk 設定傳給tr函式時的預設字串編碼,gui設計中最常用的一種 qtex...
Qt編碼風格
如果它使你的 看起來不好,你可以打破任何乙個規則。wrong int a,b char c,d correct int height int width char nameofthis char nameofthat wrong short cntr char item delim t correc...
QT編碼風格
如果它使你的 看起來不好,你可以打破任何乙個規則。wrong int a,b char c,d correct int height int width char nameofthis char nameofthat wrong short cntr char item delim t correc...