Question

I have been sitting on the same problem for over three days now and I cannot figure out what is wrong with my code.

Im my program, I use a QWebView window to represent HTML content. This HTML content is generated on-the-fly, based on user settings. I use QXmlStreamWriter to generate HTML file. QXmlStream needs a QIODevice, so I create a QByteArray, write HTML code into it and in the end I create a QString out of the QByteArray. When I print this code in the QWebView, it does not display special symbols (like German Umlauts or Russian letters) correctly. However, if I save the generated code to an HTML file, Firefox opens displays these characters correctly.

The generated HTML contains <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> tag.

I have already tried setting a codec for strings, and converting string to different formats and playing around the QByteArray itself. I use Qt 4.7.3. for Windows Desktop with MinGW 4.4.

I guess, I am missing an important point in the encoding story, and I would be grateful for any help! The problem is really annoying and it is preventing me from completing my idea! Thank you!

Was it helpful?

Solution

QXmlStreamWriter can very well write to QString directly (since it was introduced in Qt 4.3), no need to add the extra chance of getting the encoding wrong with a QByteArray.

E.g. this (trivial) snippet works just fine (the source file is encoded in UTF-8):

#include <QXmlStreamWriter>
#include <QApplication>
#include <QWebView>

int main(int argc, char **argv)
{
    QApplication app(argc, argv);

    QString bytes;
    QXmlStreamWriter xs(&bytes);

    xs.writeDTD("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
    xs.writeStartElement("html");
    xs.writeStartElement("body");
    xs.writeTextElement("h1", QString::fromUtf8("éëè абвгдеёж 漢字"));
    xs.writeEndElement();
    xs.writeEndElement();

    QWebView w;
    w.setHtml(bytes);
    w.show();
    return app.exec();
}

Without more details of your problem (code snippets and data captures - your source data encoding might not be what you think it is), can't help much more than this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top