Results 1 to 13 of 13

Thread: From QString to QWebView.

  1. #1
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default From QString to QWebView.

    Hey guys,

    I want to convert a QString to utf8 html compliant so it can be viewed in a QWebPage.

    Qt Code:
    1. QTextCodec *codec = QTextCodec::codecForName("UTF-8");
    2. QByteArray string = codec->fromUnicode(output);
    To copy to clipboard, switch view to plain text mode 

    I tried the above with no succes.

  2. #2
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: From QString to QWebView.

    I am not sure what QWebPage got to do with UTF.
    Though to convert QString to UTF, use this:
    Qt Code:
    1. QString::toUtf8();
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: From QString to QWebView.

    Consider this:

    Qt Code:
    1. QByteArray string = string.toUtf8();
    2.  
    3. for (int i = 0; i < string.size(); i++)
    4. {
    5. qDebug("%c", string.at(i));
    6. }
    To copy to clipboard, switch view to plain text mode 
    That doesn't print the UTF8 byte sequence.

  4. #4
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: From QString to QWebView.

    You can use:
    Qt Code:
    1. qDebug() << yourString
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: From QString to QWebView.

    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

  6. #6
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: From QString to QWebView.

    Qt Code:
    1. QByteArray ba("Hello world");
    2. char *data = ba.data();
    3. while (*data) {
    4. cout << "[" << *data << "]" << endl;
    5. ++data;
    6. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: From QString to QWebView.

    Qt Code:
    1. QByteArray ba(QString("©").toUtf8());
    2. char *data = ba.data();
    3. while (*data)
    4. {
    5. qkDebug("[%c]", *data);
    6. ++data;
    7. }
    To copy to clipboard, switch view to plain text mode 

    Output:

    Qt Code:
    1. [Â][©]
    To copy to clipboard, switch view to plain text mode 

    This is not the conversion I want.

    I expect "[&][#][1][6][9][;]";.
    Last edited by bunjee; 27th August 2009 at 10:08.

  8. #8
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: From QString to QWebView.

    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.

    Qt Code:
    1. QByteArray ba(QString("©").toUtf8());
    2. char *data = ba.data();
    3. while (*data)
    4. {
    5. printf("%x", *data);
    6. ++data;
    7. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: From QString to QWebView.

    Thans for the replies,

    EDIT: What I want is an UTF8 html encoded string.
    Last edited by bunjee; 27th August 2009 at 10:19.

  10. #10
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: From QString to QWebView.

    Quote Originally Posted by bunjee View Post
    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 :
    Quote Originally Posted by bunjee View Post
    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.

  11. The following user says thank you to yogeshgokul for this useful post:

    bunjee (27th August 2009)

  12. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: From QString to QWebView.

    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:
    Qt Code:
    1. QString out;
    2. for(int i=0; i<in.size();i++){
    3. QChar c = in[i];
    4. if(c.toAscii()>127){
    5. out.append(QString("&#%1;").arg((int)c.toAscii()));
    6. } else out += c;
    7. }
    To copy to clipboard, switch view to plain text mode 

    This is a rough implementation which will not work for multi-octet characters - you need to adjust the if block for that.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. The following user says thank you to wysota for this useful post:

    bunjee (27th August 2009)

  14. #12
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: From QString to QWebView.

    Nothing to add,

    I missed the point on this one.
    Thanks guys.

  15. #13
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: From QString to QWebView.

    Here is a first shot of a "toHtmlUtf8" method in my inherited QString class:

    Qt Code:
    1. QString qkString::toHtmlUtf8() const
    2. {
    3. QString result;
    4. for (int i = 0; i < size(); i++)
    5. {
    6. QChar c = at(i);
    7. char ascii = c.toAscii();
    8.  
    9. if (ascii == ' ')
    10. {
    11. result += "&nbsp;";
    12. }
    13. else if (ascii == '\n')
    14. {
    15. result += "<br>";
    16. }
    17. else if (ascii < 48 || ascii > 122)
    18. {
    19. QByteArray bytes = QString(c).toUtf8();
    20.  
    21. if (bytes.size() == 1)
    22. {
    23. result += QString("&#%1;").arg(QString::number(bytes.at(0)));
    24. }
    25. else
    26. {
    27. // FIXME
    28. // Insert multi byte code here
    29. }
    30. else result += "[?]";
    31. }
    32. else result += c;
    33. }
    34.  
    35. return result;
    36. }
    To copy to clipboard, switch view to plain text mode 

    Little help on the FIXME wouldn't hurt.

Similar Threads

  1. Qy 4.4.3 MySQL driver failed
    By pamalite in forum Installation and Deployment
    Replies: 2
    Last Post: 23rd January 2010, 01:09
  2. File rename detection
    By bunjee in forum Qt Programming
    Replies: 6
    Last Post: 23rd July 2009, 15:22
  3. Custom Model Advice Requested
    By mclark in forum Qt Programming
    Replies: 3
    Last Post: 18th September 2008, 16:26
  4. easiest Way QString can do
    By baray98 in forum Qt Programming
    Replies: 12
    Last Post: 15th April 2008, 20:49
  5. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 17:59

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.