QXmlStreamReader Codec

  • Noob question here:
    Does QXmlStreamReader auto detect the text codec based on the start document (<?xml version="1.0" encoding="UTF-8"?>)? I noticed there is no setCodec method while QXmlStreamWriter has it so I want to make 100% sure it's dealt with automatically.

  • Moderators

    Hi! Yes, the codec is automatically set depending on the encoding attribute if QT_NO_TEXTCODEC is not defined. See qxmlstream.cpp:

    #ifdef QT_NO_TEXTCODEC
                    readBuffer = QString::fromLatin1(rawReadBuffer.data(), nbytesread);
                    QTextCodec *const newCodec = QTextCodec::codecForName(name.toLatin1());
                    if (!newCodec)
                        err = QXmlStream::tr("Encoding %1 is unsupported").arg(name);
                    else if (newCodec != codec && !lockEncoding) {
                        codec = newCodec;
                        delete decoder;
                        decoder = codec->makeDecoder();
                        decoder->toUnicode(&readBuffer, rawReadBuffer.data(), nbytesread);
    #endif // QT_NO_TEXTCODEC

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.