Dmitro
Dmitro

Reputation: 1960

QXmlStreamWriter and cyrillic

I have a problem with encoding when writing XML files via QXmlStreamWriter in windows, how can I resolve it? Using stream.setCodec("UTF-8") or "windows-1251" is not helped.

QFile *file = new QFile(filename);
if (file->open(QIODevice::WriteOnly | QIODevice::Text))
         {
         QXmlStreamWriter stream(file);
         stream.setAutoFormatting(true);
         stream.writeStartDocument();
         stream.writeStartElement("СЕКЦИЯ"); // start root section
            stream.writeStartElement("FIELD");
            stream.writeAttribute("name", "Имя");
            stream.writeAttribute("value", "Иван");
            stream.writeEndElement();
         stream.writeEndElement(); // END СЕКЦИЯ
         file->close();
}

Upvotes: 0

Views: 572

Answers (1)

Frank Osterfeld
Frank Osterfeld

Reputation: 25155

Most likely the interpretation of the string literals in your source file is the problem, not the configuration of the stream writer. Make sure your source file is encoded in UTF-8 and use QString::fromUtf8("Imja") etc. (Imja in cyrillic of course) instead of the implicit literal to QString conversion.

Upvotes: 1

Related Questions