c++qtnon-ascii-charactersqfileqtextstream

Qt: Writing umlaut to file


I am trying to store the contents of a QDomDocument to a file. The document contains a German umlaut, which doesn't get saved to the file correctly.

My QDomDocument "document" is structured like this:

<parent>
    <child attribute="äüö"/>
</parent>

I save it to an XML file like this:

QString string = document.toString();
QFile file("/path/to/my/file.xml");
file.open(QIODevice::WriteOnly | QIODevice::Text)
QTextStream txtStream(&file);
txtStream<< string;
file.close();

qDebugging the string at that point reveals that the umlauts are still intact. But when writing them to a file, my XML file looks like this:

<parent>
    <child attribute="הצ"/>
</parent>

I tried various possibilities like converting the QString to a different encoding, or setting the stream codec to a different value, but the best I could get was this:

<parent>
    <child attribute="ֳ₪ֳ¼ֳ¶"/>
</parent>

which is even worse.

Please help.


Solution

  • Changing the QString to a QByteArray by using document.toByteArray() worked.

    Thanks @talamaki!