Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在Qt中设置XML文件的编码?_C++_Xml_Qt - Fatal编程技术网

C++ 如何在Qt中设置XML文件的编码?

C++ 如何在Qt中设置XML文件的编码?,c++,xml,qt,C++,Xml,Qt,我在Qt中编写了一个XML文件: QString fname = "L.xml"; QFile file(fname); if (file.open(QIODevice::WriteOnly)) { QTextStream streamFileOut(&file); streamFileOut.setCodec("Windows-1251"); QString string; QXmlStreamWriter xmlWriter(&strin

我在Qt中编写了一个XML文件:

QString fname = "L.xml";
QFile file(fname);
if (file.open(QIODevice::WriteOnly)) {

    QTextStream streamFileOut(&file);
    streamFileOut.setCodec("Windows-1251");

    QString string;

    QXmlStreamWriter xmlWriter(&string);
    xmlWriter.setAutoFormatting(true);
    xmlWriter.writeStartDocument();
        xmlWriter.writeStartElement("LIST");

            xmlWriter.writeStartElement("V");
            xmlWriter.writeCharacters("Привет");
            xmlWriter.writeEndElement();

            xmlWriter.writeStartElement("S");
            xmlWriter.writeCharacters("Привет");
            xmlWriter.writeEndElement();


        xmlWriter.writeEndElement();
    xmlWriter.writeEndDocument();

    streamFileOut << string;
    streamFileOut.flush();
    file.close();
}
QString fname=“L.xml”;
QFile文件(fname);
if(file.open(QIODevice::WriteOnly)){
QTextStream streamFileOut(&file);
setCodec(“Windows-1251”);
QString字符串;
QXmlStreamWriter(字符串)(&string);
setAutoFormatting(true);
xmlWriter.writeStartDocument();
writeStart元素(“列表”);
xmlWriter.WriteStarteElement(“V”);
xmlWriter.writeCharacters(“Пццццц”);
xmlWriter.writeEndElement();
xmlWriter.WriteStarteElement(“S”);
xmlWriter.writeCharacters(“Пццццц”);
xmlWriter.writeEndElement();
xmlWriter.writeEndElement();
xmlWriter.writeEndDocument();
streamFileOut使用,但您不能流到
QString
并保留编码属性,如文档所述。直接到文件,它可以工作:

#包括
#包括
#包括
int main()
{
自动文件=QFile{“L.xml”};
if(file.open(QIODevice::WriteOnly))
{
auto-xmlWriter=QXmlStreamWriter{&file};
setAutoFormatting(true);
setCodec(“windows-1251”);
xmlWriter.writeStartDocument();
{
writeStart元素(“列表”);
{
xmlWriter.WriteStarteElement(“V”);
{
xmlWriter.writeCharacters(u8“ППцццццц”);
}
xmlWriter.writeEndElement();
xmlWriter.WriteStarteElement(“S”);
{
xmlWriter.writeCharacters(u8“ППцццццц”);
}
xmlWriter.writeEndElement();
}
xmlWriter.writeEndElement();
}
xmlWriter.writeEndDocument();
}
}
收益率:


Привет
Привет

你真的需要指定编码吗?我相信,解析器应该自动“猜测”编码。你会在这里找到
QXmlStreamWriter
类的文档:它有你问题的答案。碰巧我自己不知道,但我只是查了一下(只键入了“QXmlStreamWriter”)在我的浏览器中的谷歌搜索框中,在几秒钟内就得到了答案。知道哪里可以找到和如何阅读技术文档是每个C++开发人员必备的技能。
<?xml version="1.0"?>
<LIST>
    <V>Привет</V>
    <S>Привет</S>
</LIST>
<?xml version="1.0" encoding="windows-1251" ?>
<LIST>
    <V>Привет</V>
    <S>Привет</S>
</LIST>