Qt工作笔记-QXmlStreamReader中的字符编码的坑
生活随笔
收集整理的這篇文章主要介紹了
Qt工作笔记-QXmlStreamReader中的字符编码的坑
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
以本人這篇博文的源碼為例:
https://blog.csdn.net/qq78442761/article/details/80501909
?
源碼改成如下所示:
#include <QApplication> #include <QDebug> #include <QFile> #include <QVector> #include <QXmlStreamReader>int main(int argc, char *argv[]) {QApplication a(argc, argv);QFile file("C:\\Users\\Administrator\\Desktop\\DrawPic\\1.dat");if(!file.open(QFile::ReadOnly|QFile::Text)){qDebug()<<"讀取XML文件時,文件打開失敗";return false;}QXmlStreamReader reader;reader.setDevice(&file);while(!reader.atEnd()){reader.readNext();qDebug()<<reader.text();}file.close();return a.exec(); }?
xml文件就是這樣的
<test><data1><name1>你妹</name1><name2>你妹妹</name2><name3>你妹妹妹</name3><name4>你妹妹妹妹</name4><name5>你妹妹妹妹妹</name5><double>3.1415</double><int1>10</int1><attribution include="100"><int3>1000</int3></attribution></data1><data2><name1>你姐</name1><name2>你姐姐</name2><name3>你姐姐姐</name3><name4>你姐姐姐姐</name4><name5>你姐姐姐姐姐</name5><double>6.6666</double><int1>66</int1><attribution include="666"><int3>66666</int3></attribution></data2> </test>把這個編碼改成ANSI編碼存儲,如下圖所示:
<?xml ? version="1.0" ? encoding="GBK"?>
這一行是去掉了的,
?
此時是讀不了數據的,因為Qt Creator 默認采用UTF-8編碼,而setCodec這個函數在QXmlStreamReader中并沒有,所有讀不了。
?
對于這個坑有以下幾個方法:
1.修改程序字符編碼;
2.加上encoding,讓QXmlStreamReader知道應該采用咋樣的編碼;
3.保存文件的時候,采用UTF-8的格式存儲
總結
以上是生活随笔為你收集整理的Qt工作笔记-QXmlStreamReader中的字符编码的坑的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt工作笔记-QGraphicsView
- 下一篇: C++ opengl GL_QUAD_S