PDA

View Full Version : How to disply Simplified Chinese in QTextBrowser



xjtu
21st April 2008, 07:58
When I tried to disply some html code (coded using "text/css ,Gb2312") in a textbrowser !
It just shows garbled code like this。:confused:

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 !

wysota
21st April 2008, 08:15
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).