I'm having trouble getting the hang of the character encoding while dealing with QWebKit's QWebElement
and its toPlainText()
function (*).
I have got a QString
with UTF8 encoding holding the content of a HTML page, which was read from local disc via QFile
. No I want to parse this page by using QWebKit. Thus I defined a QWebFrame
object as part of a QWebPage
. With QWebFrame::setHtml()
I filled in the QString into the QWebKit environment.
QString rawReport = "some UTF8 encoded string read in previously";
QWebPage p;
QWebFrame *frame = p.mainFrame();
frame->setHtml(rawReport);
QWebElement report = frame->documentElement();
qDebug() << report.toPlainText();
But somehow, qDebug()
seems to get the encoding wrong as for example German umlauts äöüß
are shown rather funny. Even not as their corresponding HTML entities.
I doubt it's qDebug's fault but rather the encoding inside QWebElement. Somewhere I read, that QWebFrame::setHtml()
expects UTF8 encoding. But I'm almost sure, this is the case here.
What am I missing? Is there somewhere a function/option to force QWebFrame/QWebElement to use a specific character encoding for both, input and output?
QWebElement::toOuterXml()
or QWebElement::toInnerXml()
show the same encoding problem.
Have you tried using from***() functions of QString to find how the string returned by toPlainText() is encoded?
The documentation states
When using this method WebKit assumes that external resources such as JavaScript programs or style sheets are encoded in UTF-8 unless otherwise specified. For example, the encoding of an external script can be specified through the charset attribute of the HTML script tag. It is also possible for the encoding to be specified by web server.''.
I would thus try to change the charset specified in the html source (in the corresponding meta tag) that you are loading to explicitly specify that you are using UTF-8.