View Full Version : From QString to QWebView.
bunjee
27th August 2009, 01:27
Hey guys,
I want to convert a QString to utf8 html compliant so it can be viewed in a QWebPage.
QTextCodec *codec = QTextCodec::codecForName("UTF-8");
QByteArray string = codec->fromUnicode(output);
I tried the above with no succes.
yogeshgokul
27th August 2009, 05:52
I am not sure what QWebPage got to do with UTF.
Though to convert QString to UTF, use this:
QString::toUtf8();
bunjee
27th August 2009, 09:04
Consider this:
QByteArray string = string.toUtf8();
for (int i = 0; i < string.size(); i++)
{
qDebug("%c", string.at(i));
}
That doesn't print the UTF8 byte sequence.
yogeshgokul
27th August 2009, 09:11
You can use:
qDebug() << yourString
bunjee
27th August 2009, 09:22
UTF-8 encodes each character (code point) in 1 to 4 octets (8-bit bytes), with the single octet encoding used only for the 128 US-ASCII characters
I want to print the full byte sequence.
see http://www.tony-franks.co.uk/UTF-8.htm
yogeshgokul
27th August 2009, 09:29
QByteArray ba("Hello world");
char *data = ba.data();
while (*data) {
cout << "[" << *data << "]" << endl;
++data;
}
bunjee
27th August 2009, 09:57
QByteArray ba(QString("©").toUtf8());
char *data = ba.data();
while (*data)
{
qkDebug("[%c]", *data);
++data;
}
Output:
[Â][©]
This is not the conversion I want.
I expect "[&][#][1][6][9][;]";.
yogeshgokul
27th August 2009, 10:01
You have used %c, and it will print the character represented by byte. ;) If you want a hexadecimal byte representation of your string. Why dont you use old school printf.
QByteArray ba(QString("©").toUtf8());
char *data = ba.data();
while (*data)
{
printf("%x", *data);
++data;
}
bunjee
27th August 2009, 10:07
Thans for the replies,
EDIT: What I want is an UTF8 html encoded string.
yogeshgokul
27th August 2009, 10:20
I want.
I expect "[&][#][1][6][9][;]";.
If you can theoretically prove it.
I will give you code.
When you want to print a byte sequence, you have to decide the representation. A possible representation is hex. That I already told you how to do that. But your expected result is :
I expect "[&][#][1][6][9][;]";.
I dont know what is it. Can you please tell me whats in side []. a character/hex value/int /or something else.
wysota
27th August 2009, 10:21
You want the sgml entity, not a "utf-8 sequence" - there is no such thing.
Qt will not encode sgml entities by itself. You need to do that manually:
QString in;
QString out;
for(int i=0; i<in.size();i++){
QChar c = in[i];
if(c.toAscii()>127){
out.append(QString("&#%1;").arg((int)c.toAscii()));
} else out += c;
}
This is a rough implementation which will not work for multi-octet characters - you need to adjust the if block for that.
bunjee
27th August 2009, 10:25
Nothing to add,
I missed the point on this one.
Thanks guys.
bunjee
28th August 2009, 01:17
Here is a first shot of a "toHtmlUtf8" method in my inherited QString class:
QString qkString::toHtmlUtf8() const
{
QString result;
for (int i = 0; i < size(); i++)
{
QChar c = at(i);
char ascii = c.toAscii();
if (ascii == ' ')
{
result += " ";
}
else if (ascii == '\n')
{
result += "<br>";
}
else if (ascii < 48 || ascii > 122)
{
QByteArray bytes = QString(c).toUtf8();
if (bytes.size() == 1)
{
result += QString("&#%1;").arg(QString::number(bytes.at(0)));
}
else
{
// FIXME
// Insert multi byte code here
}
else result += "[?]";
}
else result += c;
}
return result;
}
Little help on the FIXME wouldn't hurt.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.