Qt: QWebView and QByteArray cause encoding conflict

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!

Answers


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.


Need Your Help

Creating Xml file using Android

android

I tried this code but i don't know where I will find the xml file created

How to return a void statement from -id in iOS?

iphone ios xcode methods void

In my code I run an -(id) code and the general method is to return a string or boolean value. Is there a way that I can return a method instead? In the code I have several 'if' statements, and wo...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.