qt 保存文件为utf8

转载:https://www.cnblogs.com/cppskill/p/7999800.html

 1 bool TdrawSvg::Save2File(char* _pcFullFileName)
 2 {
 3     // http://blog.csdn.net/u011314012/article/details/49861363
 4     // https://stackoverflow.com/questions/4780507/create-utf-8-file-in-qt
 5 
 6     QString str = FpOwnerWebView->page()->currentFrame()->toHtml();
 7     //QString unicodeString = "Some Unicode string";
 8     QFile fileOut("C:\\zzz.svg");
 9     //if (!fileOut.open(QIODevice::WriteOnly | QIODevice::Text | QFile::Truncate))
10     if (! fileOut.open(QFile::WriteOnly | QFile::Truncate))
11         return false;
12 
13     QTextStream streamFileOut(&fileOut);
14     streamFileOut.setCodec("UTF-8");
15     streamFileOut << str;
16     streamFileOut.flush();
17 
18     // ZC: 查到说,下面的参数是true的话 就是保存成"UTF-8 + BOM",false的话 就是保存成"UTF-8"
19     //  但是,我尝试了一下,都是"UTF-8"... 不知为啥...
20     streamFileOut.setGenerateByteOrderMark(true);
21 
22     fileOut.close();
23 }

 

posted @ 2019-07-05 10:17  阳光下的小土豆  阅读(1347)  评论(0编辑  收藏  举报