Results 1 to 2 of 2

Thread: How to disply Simplified Chinese in QTextBrowser

  1. #1
    Join Date
    Apr 2008
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Question How to disply Simplified Chinese in QTextBrowser

    When I tried to disply some html code (coded using "text/css ,Gb2312") in a textbrowser !
    It just shows garbled code like this。

    Hrer is the code

    QTexrBrowser ts=new QTextBrowser();
    QString html,css;
    ts->setOpenExternalLinks(true);
    QTextCodec::setCodecForTr(QTextCodec::codecForName ("GB2312"));
    /*
    * creat html code*/

    html=
    "<html><head>"
    "<meta http-equiv='Content-Type' content='text/html; charset=gb2312'>"
    "<title>XJTU PORTAL</title><type='text/css' href='format.css'>"//important
    "</head><body>"
    "<img src='xjtu_logo.gif' width='199' height='45' align='absmiddle'><br>"
    "<div align='center' class='style1'> <p>西安交大门户系统常用链接</p></div>"
    "<p><span class='style2'><a href='http://www.xjtu.edu.cn'>交大主页</a></span></p>"
    "<p class='style2'><a href='http://stu.xjtu.edu.cn'>学生邮箱</a></p>"
    "<p class='style2'><a href='http://bbs.xjtu.edu.cn'>兵马俑BBS </a></p>"
    "</body></html>" ;
    /*creat css code used in html*/
    css = ".style1 {color: #FF0000}"
    ".style2 {color: #00FF00}";
    // Crate a QTextDocument with the defined HTML, CSS and the images
    QTextDocument *doc = new QTextDocument;
    /*
    * This shows how to bind the images, where the name for QUrl is the same name
    * which you use in your HTML and CSS. In QPixmap just use the normal syntax of
    * the Qt resource system.
    */
    doc->addResource( QTextDocument::ImageResource, QUrl( "xjtu_logo.gif" ), QPixmap( "iconsets/system/default/portal/xjtu_logo.gif" ) );

    /*
    * And now bind the css, which you have defined in the QString css.
    */
    doc->addResource( QTextDocument::StyleSheetResource, QUrl( "format.css" ), css );
    doc->setHtml( html ); // binds the HTML to the QTextDocument
    ts->setDocument(doc);

    /************************************/

    The red code is write in Simplified chinese !
    Last edited by xjtu; 21st April 2008 at 08:36. Reason: provide the source code

  2. #2
    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: How to disply Simplified Chinese in QTextBrowser

    You have to provide the text in appropriate encoding or tell Qt which encoding to use. Take a look at QString::from* methods and QTextCodec class. If you are displaying html from an outside source (like a file) it should be enough to provide the encoding used in the html file itself. If that doesn't work, you'll have to convert your resource to utf-8 or unicode (but it shouldn't come to that).

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.