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 @   阳光下的小土豆  阅读(1362)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示