[Interest] QDateTime + QXmlStreamWriter on non-UTF8 compatible systems

Scott Bloom scott at towel42.com
Tue Dec 26 14:48:01 CET 2017


On terça-feira, 26 de dezembro de 2017 10:01:58 -02 Scott Bloom wrote:
> When someone who has been an active user of Qt for 15+ years, says 
> there is something fundamentally missing from the documentation.

I did not dispute that. Yes, there is something missing from the documentation and it needs to be improved. I wholeheatedly agree with you there.

I am also saying there's a bug somewhere else in your code or in Qt. The problem is not the Writer, but the transformations you made before sending the information to the reader or the Reader itself.

> void QXmlStreamWriter::​setCodec(QTextCodec * codec) Sets the codec 
> for this stream to codec. The codec is used for encoding any data that 
> is written. By default, QXmlStreamWriter uses UTF-8.
 
> The encoding information is stored in the initial xml tag which gets 
> written when you call writeStartDocument(). Call this function before 
> calling writeStartDocument().
 
> If the documentation said: Note, when using a QString as a target, the 
> codec setting is ignored.  Since QString is always UTF-16.
 
> Then I would never had asked any question to the mailing list.

I'll add that. See https://codereview.qt-project.org/215337

============
I would add a note along the lines, of "see setCodec" for the QXmlStreamWriter( QString * ) constructor.  

As  to the bug, it was clear (once I understood the issue at hand) that writing to a QString and then writing the QString to a QFile via QTextStream was the wrong approach.  Since the final destination being 8 bit, would be never be thought of in the writer.  The purpose was to enable easy unit testing that didn’t required file IO.

Ie, in our unit test
QString xml;
writeToXml( xml )
compareXmlToGolden( xml, golden); 

worked fine.  And the tool could use

QString xml;
writeToXml( xml )
QFile fi;
writeToFile( fi, xml )

The change turned out to be as easy as having the writeToXml take in a QIODevice and have the unit test use QBuffer, and the tool use the same QFile

Scott


More information about the Interest mailing list