﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-A Pisces Programmer-文章分类-Qt/C++</title><link>http://www.cppblog.com/boymaster/category/20154.html</link><description>漂亮的程序，开心地工作</description><language>zh-cn</language><lastBuildDate>Sat, 24 Nov 2012 02:47:27 GMT</lastBuildDate><pubDate>Sat, 24 Nov 2012 02:47:27 GMT</pubDate><ttl>60</ttl><item><title>QtWebkit类库与Native代码的混合开发</title><link>http://www.cppblog.com/boymaster/articles/194963.html</link><dc:creator>双鱼座的程序员</dc:creator><author>双鱼座的程序员</author><pubDate>Fri, 09 Nov 2012 02:45:00 GMT</pubDate><guid>http://www.cppblog.com/boymaster/articles/194963.html</guid><wfw:comment>http://www.cppblog.com/boymaster/comments/194963.html</wfw:comment><comments>http://www.cppblog.com/boymaster/articles/194963.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/boymaster/comments/commentRss/194963.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/boymaster/services/trackbacks/194963.html</trackback:ping><description><![CDATA[<div>在QT里面，我们可以很方便地调用QtWebKit类库，来实现一个浏览器的开发<br />基于Html的界面在开发效率，可移植性上都十分有优势，所以也被很多程序采用，只是我们平时没注意到而已<br /><br />下面先说一下，如何创建一个简易的浏览器<br />通过VS2008+QTSDK，创建一个qt4 GUI工程:<br /><img border="0" alt="" src="http://www.cppblog.com/images/cppblog_com/boymaster/01.jpg" /><br />记住，一定要选上QtWebKit和QNetwork(这个后面要用到)...<br /><br />写如下一段代码，一个浏览器就做成了:<br /><strong>main.cpp</strong> 
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">QtGui</span><span style="color: #000000">/</span><span style="color: #000000">QApplication</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;2</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">QWebView</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;3</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">QMainWindow</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;4</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /></span><span style="color: #008080">&nbsp;5</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;argc,&nbsp;</span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">argv[])<br /></span><span style="color: #008080">&nbsp;6</span><span style="color: #000000"><img id="Codehighlighter1_107_330_Open_Image" onclick="this.style.display='none'; Codehighlighter1_107_330_Open_Text.style.display='none'; Codehighlighter1_107_330_Closed_Image.style.display='inline'; Codehighlighter1_107_330_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_107_330_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_107_330_Closed_Text.style.display='none'; Codehighlighter1_107_330_Open_Image.style.display='inline'; Codehighlighter1_107_330_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_107_330_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_107_330_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;7</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;QApplication&nbsp;a(argc,&nbsp;argv);<br /></span><span style="color: #008080">&nbsp;8</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;QMainWindow&nbsp;window;<br /></span><span style="color: #008080">&nbsp;9</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;QWebView&nbsp;view(</span><span style="color: #000000">&amp;</span><span style="color: #000000">window);<br /></span><span style="color: #008080">10</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;view.setGeometry(</span><span style="color: #000000">0</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">600</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">400</span><span style="color: #000000">);<br /></span><span style="color: #008080">11</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;view.setUrl(QUrl(</span><span style="color: #000000">"</span><span style="color: #000000">http://www.cppblog.com/boymaster</span><span style="color: #000000">"</span><span style="color: #000000">));<br /></span><span style="color: #008080">12</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;window.show();<br /></span><span style="color: #008080">13</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;a.exec();<br /></span><span style="color: #008080">14</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">15</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span></div>QWebView有两种方法可以用来设定要显示的内容，一个是setUrl方法，一个是setContent方法。 这个很简单，试一下就会，不多说了<br /><br />进入正题了，接下来介绍一下，如何实现js与C++的双向调用<br />这里，仅介绍开发中用到的一种调用方式，&nbsp;用QWebFrame的addToJavaScriptWindowObject方法<br />个人比较推荐这种方式，易为理解~~~<br /><br />1. JS如何调用C++函数，详见代码~~~<br /><strong>myobject.h</strong><br />
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">#ifndef&nbsp;MYOBJECT_H<br /></span><span style="color: #008080">&nbsp;2</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">#define</span><span style="color: #000000">&nbsp;MYOBJECT_H</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;3</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /></span><span style="color: #008080">&nbsp;4</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">QObject</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;5</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">QWebPage</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;6</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">QWebFrame</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;7</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /></span><span style="color: #008080">&nbsp;8</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;!!&nbsp;ATTENTION&nbsp;!!&nbsp;:&nbsp;The&nbsp;object&nbsp;do&nbsp;NOT&nbsp;need&nbsp;to&nbsp;inherit&nbsp;from&nbsp;QWidget&nbsp;anymore.</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;9</span><span style="color: #008000"><img id="Codehighlighter1_205_450_Open_Image" onclick="this.style.display='none'; Codehighlighter1_205_450_Open_Text.style.display='none'; Codehighlighter1_205_450_Closed_Image.style.display='inline'; Codehighlighter1_205_450_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_205_450_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_205_450_Closed_Text.style.display='none'; Codehighlighter1_205_450_Open_Image.style.display='inline'; Codehighlighter1_205_450_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="color: #0000ff">class</span><span style="color: #000000">&nbsp;MyObject&nbsp;:</span><span style="color: #0000ff">public</span><span style="color: #000000">&nbsp;QObject&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_205_450_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_205_450_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">10</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;Q_OBJECT<br /></span><span style="color: #008080">11</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #0000ff">private</span><span style="color: #000000">:<br /></span><span style="color: #008080">12</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;QWebPage&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">page;<br /></span><span style="color: #008080">13</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #0000ff">public</span><span style="color: #000000">:<br /></span><span style="color: #008080">14</span><span style="color: #000000"><img id="Codehighlighter1_299_301_Open_Image" onclick="this.style.display='none'; Codehighlighter1_299_301_Open_Text.style.display='none'; Codehighlighter1_299_301_Closed_Image.style.display='inline'; Codehighlighter1_299_301_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_299_301_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_299_301_Closed_Text.style.display='none'; Codehighlighter1_299_301_Open_Image.style.display='inline'; Codehighlighter1_299_301_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;MyObject(QWebPage&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">page)&nbsp;:&nbsp;page(page)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_299_301_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_299_301_Open_Text"><span style="color: #000000">{&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">15</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #0000ff">public</span><span style="color: #000000">&nbsp;slots:<br /></span><span style="color: #008080">16</span><span style="color: #000000"><img id="Codehighlighter1_344_448_Open_Image" onclick="this.style.display='none'; Codehighlighter1_344_448_Open_Text.style.display='none'; Codehighlighter1_344_448_Closed_Image.style.display='inline'; Codehighlighter1_344_448_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_344_448_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_344_448_Closed_Text.style.display='none'; Codehighlighter1_344_448_Open_Image.style.display='inline'; Codehighlighter1_344_448_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;func(QString&nbsp;arg)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_344_448_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_344_448_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">17</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">this</span><span style="color: #000000">-&gt;</span><span style="color: #000000">page</span><span style="color: #000000">-&gt;</span><span style="color: #000000">mainFrame()</span><span style="color: #000000">-&gt;</span><span style="color: #000000">evaluateJavaScript(</span><span style="color: #000000">"</span><span style="color: #000000">document.body.innerHTML&nbsp;+=&nbsp;'</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;arg&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">';</span><span style="color: #000000">"</span><span style="color: #000000">);<br /></span><span style="color: #008080">18</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">19</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000">;<br /></span><span style="color: #008080">20</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /></span><span style="color: #008080">21</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">#endif</span><span style="color: #000000">&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;MYOBJECT_H</span><span style="color: #000000"><br /></span><span style="color: #008080">22</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span></div><strong><br />main.cpp<br /></strong>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">QtGui</span><span style="color: #000000">/</span><span style="color: #000000">QApplication</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;2</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">QMainWindow</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;3</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">QWebView</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;4</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">QWebPage</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;5</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">QWebFrame</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;6</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">MyObject.h</span><span style="color: #000000">"</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;7</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /></span><span style="color: #008080">&nbsp;8</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;argc,&nbsp;</span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">argv[])<br /></span><span style="color: #008080">&nbsp;9</span><span style="color: #000000"><img id="Codehighlighter1_170_666_Open_Image" onclick="this.style.display='none'; Codehighlighter1_170_666_Open_Text.style.display='none'; Codehighlighter1_170_666_Closed_Image.style.display='inline'; Codehighlighter1_170_666_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_170_666_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_170_666_Closed_Text.style.display='none'; Codehighlighter1_170_666_Open_Image.style.display='inline'; Codehighlighter1_170_666_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_170_666_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_170_666_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">10</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;QApplication&nbsp;a(argc,&nbsp;argv);<br /></span><span style="color: #008080">11</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;QMainWindow&nbsp;window;<br /></span><span style="color: #008080">12</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;QWebView&nbsp;view(</span><span style="color: #000000">&amp;</span><span style="color: #000000">window);<br /></span><span style="color: #008080">13</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;QWebPage&nbsp;page;<br /></span><span style="color: #008080">14</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;view.setPage(</span><span style="color: #000000">&amp;</span><span style="color: #000000">page);<br /></span><span style="color: #008080">15</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;view.setGeometry(</span><span style="color: #000000">0</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">600</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">400</span><span style="color: #000000">);<br /></span><span style="color: #008080">16</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;MyObject&nbsp;obj(</span><span style="color: #000000">&amp;</span><span style="color: #000000">page);<br /></span><span style="color: #008080">17</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;page.mainFrame()</span><span style="color: #000000">-&gt;</span><span style="color: #000000">addToJavaScriptWindowObject(</span><span style="color: #000000">"</span><span style="color: #000000">qt</span><span style="color: #000000">"</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">&amp;</span><span style="color: #000000">obj);<br /></span><span style="color: #008080">18</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;QString&nbsp;content(</span><span style="color: #000000">"</span><span style="color: #000000">&lt;script&gt;function&nbsp;f()&nbsp;{&nbsp;qt.func('http://www.cppblog.com/boymaster');&nbsp;}&lt;/script&gt;</span><span style="color: #000000">"</span><span style="color: #000000">);<br /></span><span style="color: #008080">19</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;content&nbsp;</span><span style="color: #000000">+=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">&lt;a&nbsp;href='javascript:f()'&gt;Click&nbsp;Me&lt;/a&gt;</span><span style="color: #000000">"</span><span style="color: #000000">;<br /></span><span style="color: #008080">20</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;view.setContent(content.toAscii());<br /></span><span style="color: #008080">21</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;window.show();<br /></span><span style="color: #008080">22</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: #008080">23</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;a.exec();<br /></span><span style="color: #008080">24</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">25</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span></div>这样，当点击页面上的"Click Me"，script脚本用会调用到C++中的func函数~~~<br /><br />2.C++调用JS脚本<br /><strong>myobject.h</strong><br />
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span><img id="Codehighlighter1_31_517_Open_Image" onclick="this.style.display='none'; Codehighlighter1_31_517_Open_Text.style.display='none'; Codehighlighter1_31_517_Closed_Image.style.display='inline'; Codehighlighter1_31_517_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_31_517_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_31_517_Closed_Text.style.display='none'; Codehighlighter1_31_517_Open_Image.style.display='inline'; Codehighlighter1_31_517_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"><span style="color: #0000ff">class</span><span style="color: #000000">&nbsp;MyObject&nbsp;:</span><span style="color: #0000ff">public</span><span style="color: #000000">&nbsp;QObject&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_31_517_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_31_517_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;2</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;Q_OBJECT<br /></span><span style="color: #008080">&nbsp;3</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: #008080">&nbsp;4</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #0000ff">private</span><span style="color: #000000">:<br /></span><span style="color: #008080">&nbsp;5</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;QWebPage&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">page;<br /></span><span style="color: #008080">&nbsp;6</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;QTimer&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">timer;<br /></span><span style="color: #008080">&nbsp;7</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: #008080">&nbsp;8</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #0000ff">public</span><span style="color: #000000">:<br /></span><span style="color: #008080">&nbsp;9</span><span style="color: #000000"><img id="Codehighlighter1_146_279_Open_Image" onclick="this.style.display='none'; Codehighlighter1_146_279_Open_Text.style.display='none'; Codehighlighter1_146_279_Closed_Image.style.display='inline'; Codehighlighter1_146_279_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_146_279_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_146_279_Closed_Text.style.display='none'; Codehighlighter1_146_279_Open_Image.style.display='inline'; Codehighlighter1_146_279_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;MyObject(QWebPage&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">page)&nbsp;:&nbsp;page(page)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_146_279_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_146_279_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">10</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;timer&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;QTimer;<br /></span><span style="color: #008080">11</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;connect(timer,&nbsp;SIGNAL(timeout()),&nbsp;</span><span style="color: #0000ff">this</span><span style="color: #000000">,&nbsp;SLOT(slotTimerOut()));<br /></span><span style="color: #008080">12</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;timer</span><span style="color: #000000">-&gt;</span><span style="color: #000000">start(</span><span style="color: #000000">1000</span><span style="color: #000000">);<br /></span><span style="color: #008080">13</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">14</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: #008080">15</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />signals:<br /></span><span style="color: #008080">16</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;signalObjTimerOut();<br /></span><span style="color: #008080">17</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: #008080">18</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #0000ff">private</span><span style="color: #000000">&nbsp;slots:<br /></span><span style="color: #008080">19</span><span style="color: #000000"><img id="Codehighlighter1_358_448_Open_Image" onclick="this.style.display='none'; Codehighlighter1_358_448_Open_Text.style.display='none'; Codehighlighter1_358_448_Closed_Image.style.display='inline'; Codehighlighter1_358_448_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_358_448_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_358_448_Closed_Text.style.display='none'; Codehighlighter1_358_448_Open_Image.style.display='inline'; Codehighlighter1_358_448_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;connectSlots()&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_358_448_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_358_448_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">20</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;page</span><span style="color: #000000">-&gt;</span><span style="color: #000000">mainFrame()</span><span style="color: #000000">-&gt;</span><span style="color: #000000">evaluateJavaScript(</span><span style="color: #000000">"</span><span style="color: #000000">DevObject.signalDevObjTimerOut.connect(f);</span><span style="color: #000000">"</span><span style="color: #000000">);<br /></span><span style="color: #008080">21</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">22</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: #008080">23</span><span style="color: #000000"><img id="Codehighlighter1_475_515_Open_Image" onclick="this.style.display='none'; Codehighlighter1_475_515_Open_Text.style.display='none'; Codehighlighter1_475_515_Closed_Image.style.display='inline'; Codehighlighter1_475_515_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_475_515_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_475_515_Closed_Text.style.display='none'; Codehighlighter1_475_515_Open_Image.style.display='inline'; Codehighlighter1_475_515_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;slotTimerOut()&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_475_515_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_475_515_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">24</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;emit&nbsp;signalObjTimerOut();<br /></span><span style="color: #008080">25</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">26</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000">;<br /></span><span style="color: #008080">27</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /></span><span style="color: #008080">28</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">#endif</span><span style="color: #000000">&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;MYOBJECT_H</span></div><br /><strong>main.cpp</strong><br />
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;argc,&nbsp;</span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">argv[])<br /></span><span style="color: #008080">&nbsp;2</span><span style="color: #000000"><img id="Codehighlighter1_33_536_Open_Image" onclick="this.style.display='none'; Codehighlighter1_33_536_Open_Text.style.display='none'; Codehighlighter1_33_536_Closed_Image.style.display='inline'; Codehighlighter1_33_536_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_33_536_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_33_536_Closed_Text.style.display='none'; Codehighlighter1_33_536_Open_Image.style.display='inline'; Codehighlighter1_33_536_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_33_536_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_33_536_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;3</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;QApplication&nbsp;a(argc,&nbsp;argv);<br /></span><span style="color: #008080">&nbsp;4</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;QMainWindow&nbsp;window;<br /></span><span style="color: #008080">&nbsp;5</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;QWebView&nbsp;view(</span><span style="color: #000000">&amp;</span><span style="color: #000000">window);<br /></span><span style="color: #008080">&nbsp;6</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;QWebPage&nbsp;page;<br /></span><span style="color: #008080">&nbsp;7</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;view.setPage(</span><span style="color: #000000">&amp;</span><span style="color: #000000">page);<br /></span><span style="color: #008080">&nbsp;8</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;view.setGeometry(</span><span style="color: #000000">0</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">600</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">400</span><span style="color: #000000">);<br /></span><span style="color: #008080">&nbsp;9</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;MyObject&nbsp;obj(</span><span style="color: #000000">&amp;</span><span style="color: #000000">page);<br /></span><span style="color: #008080">10</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;QObject::connect(</span><span style="color: #000000">&amp;</span><span style="color: #000000">view,&nbsp;SIGNAL(loadFinished(</span><span style="color: #0000ff">bool</span><span style="color: #000000">)),&nbsp;</span><span style="color: #000000">&amp;</span><span style="color: #000000">obj,&nbsp;SLOT(connectSlots()));<br /></span><span style="color: #008080">11</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;page.mainFrame()</span><span style="color: #000000">-&gt;</span><span style="color: #000000">addToJavaScriptWindowObject(</span><span style="color: #000000">"</span><span style="color: #000000">qt</span><span style="color: #000000">"</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">&amp;</span><span style="color: #000000">obj);<br /></span><span style="color: #008080">12</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;QString&nbsp;content(</span><span style="color: #000000">"</span><span style="color: #000000">&lt;SCRIPT&gt;function&nbsp;f()&nbsp;{&nbsp;document.body.innerHTML&nbsp;+=&nbsp;'http://www.cppblog.com/boymaster';&nbsp;}&lt;/SCRIPT&gt;</span><span style="color: #000000">"</span><span style="color: #000000">);<br /></span><span style="color: #008080">13</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;view.setContent(content.toAscii());<br /></span><span style="color: #008080">14</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;window.show();<br /></span><span style="color: #008080">15</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;a.exec();<br /></span><span style="color: #008080">16</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">17</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span></div><br />这样，页面加载后，C++的Timer每隔1秒就会调用到JS的f()函数<br /></div><img src ="http://www.cppblog.com/boymaster/aggbug/194963.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/boymaster/" target="_blank">双鱼座的程序员</a> 2012-11-09 10:45 <a href="http://www.cppblog.com/boymaster/articles/194963.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>【转】利用QT进行web与本地混合应用开发</title><link>http://www.cppblog.com/boymaster/articles/194954.html</link><dc:creator>双鱼座的程序员</dc:creator><author>双鱼座的程序员</author><pubDate>Fri, 09 Nov 2012 01:30:00 GMT</pubDate><guid>http://www.cppblog.com/boymaster/articles/194954.html</guid><wfw:comment>http://www.cppblog.com/boymaster/comments/194954.html</wfw:comment><comments>http://www.cppblog.com/boymaster/articles/194954.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/boymaster/comments/commentRss/194954.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/boymaster/services/trackbacks/194954.html</trackback:ping><description><![CDATA[<p>Qt Features for Hybrid Web/Native Application Development。<br />原文参见&nbsp;<a href="http://www.qtsoftware.com/forms/whitepapers/reg-whitepaper-hybrid">http://www.qtsoftware.com/forms/whitepapers/reg-whitepaper-hybrid</a>.（限于本人英文水平，只译其大概）<br /><br /><br /><span style="font-family: Arial">Qt提供了本地 C++对象与JavaScript的无缝集成，是进行本地与web混合应用开发的理想平台。<br /><br /><br />(一）Qt Webkit 集成<br />　　利用Qt的Webkit 集成与QtNetwork模块，你完全可以进行本地桌面与web混合应用开发，你可以自由地混合JavaScript，样式表，Web内容和Qt组件。 Webkit是一个非常成熟的web浏览引擎。Qt中集成了这个大名鼎鼎的引擎，通过ＱtWebkit，你可以在Ｃ++ 中执行JavaScript，或者在网页中集成Ｃ++对象，并且通过JavaScript和这些对象进行交互。<br />　　一个现代的ＨＴＭＬ渲染引擎只是混合开发的一半，另一半就是本地应用和渲染对象的交互。ＱＴ的Webkit 集成提供了这种解决方案：<br />　　１.使用object标签嵌入Qt Widgets组件。这可以让使用Ｃ++代码的Qt组件包含在网页中，作为网页的部分外观。<br />　　２.在JavaScript中访问Ｃ++对象。你可以在JavaScript环境中插入Ｃ++对象，让网页脚本直接访问你的数据结构。<br />　　３.在Qt中执行JavaScript。你可以在Ｃ++调用网页环境中的JavaScript函数，触发网页事件。<br />　　４.共享客户端存储。在JavaScript和Ｃ++中你都具有访问数据库的能力，这样当下线时也能共享大量数据。<br /><br /><br />(二）与嵌入的Qt对象交互<br />　　使用 QWebView 组件，有两种方法可以在网页中嵌入C++对象。你可以在网页的JavaScript中添加C++对象，或者也可以创建一个插件，然后在网页中使用object标签嵌入。<br />　　<br />　　第二种方法更容易入手。当在网页中放入 Widget组件时，它的所有public slots就像普通函数一样被网页中的JavaScript函数访问。<br />&nbsp;&nbsp;&nbsp; 要在网页中添加一个Widget，首先要告诉你的QWebPage对象,该Widget可用,这个通过子类化QWebPlugFactory完成,你需要重新实现两个方法：plugs和create。plugs方法通知网页该Widget可用，create方法根据请求创建widget。<br />　　在HTML网页中，widgets使用object标签创建。比如，下面这个标签试图创建一个 application/x-qt-colorlabel　组件。</span></p>
<div>
<p>&lt;object&nbsp;type="application/x-qt-colorlabel"&nbsp;width="50px"&nbsp;height="20px"&nbsp;id="label"&nbsp;/&gt;</p></div>
<p><br />　　要利用这种创建，必须要允许使用插件并且要告诉QWebpage插件的工厂类。在下面的代码中，ColorLabelFactory将会根据application/x-qt-colorlabel的请求创建相应实例。</p>
<div>QWebSettings::globalSettings()-&gt;setAttribute(QWebSettings::PluginsEnabled,&nbsp;true);&nbsp;<br />webView-&gt;page()-&gt;setPluginFactory(new　ColorLabelFactory(this));<br /></div>
<p><br />　　ColorLabel有一个公开的slot: chagneColor(),这个对于网页中的JavaScript自动可用。在网页中插入一个指向该元素的链接，可以以一种简单的方式激活C++函数。</p>
<div>&lt;a&nbsp;href='javascript:document.getElementById("label").changeColor();'&gt;Change&nbsp;color!&lt;/a&gt;&nbsp;</div>
<p>　<br />　<br />　　要反方向推进事件，必须要使你的对象在JavaScript文档上下文中可用。要对QWebPage的每一个QWebFrame，调用addToJavaScriptWindowObject方法。这个方法允许你根据名字把一个对象添加到JavaScipt上下文中。</p>
<div>webView-&gt;page()-&gt;mainFrame()-&gt;addToJavaScriptWindowObject(&nbsp;"eventSource",&nbsp;new&nbsp;eventSource(&nbsp;this&nbsp;)&nbsp;);</div>
<p><br />　　要连接刚添加对象eventSource的信号，要加上一段JavaScript代码，使用evaluateJavaScript方法完成。下面的代码将把eventSource对象的signalName信号连接到一个JavaScript函数destFunction。</p>
<div><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" />webView-&gt;page()-&gt;mainFrame()-&gt;evaluateJavaScript(&nbsp;"eventSource.signalName.connect(destFunction);"&nbsp;);</div>
<p><br /><br />　　如果你把一个对象添加到一个以标准浏览器查看的JavaScript页面中，有一个信号需要知道。每一次JavaScript 内容被清除，Frame都会释放 javaScriptWindowObjectCleared 信号。为了使你的Qt对象一直可用，你需要连接这个信号，并且在这里调用 addToJavaScriptWindowObject函数。</p>
<p>&nbsp;</p>
<p>（二）使用客户端存储共享数据</p>
<p>&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;随着HTML5，WEB标准日益靠近于桌面，同样，桌面也开始集成WEB。这方面一个最大的变化就是客户端存储。这在客户机上给了每一方（比如每一个页面）一个能利用SQL的数据库引擎，可以缓存本地数据，减少流量，使页面能脱机使用。还可以用来存储大量的结构化的，可搜索的数据。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 客户端存储可以在JavaScript中使用，从JavaScript代码中搜索数据库，然后从搜索结果中生成页面。这要使用 openDatabase和transaction函数。</p>
<p>假设现在有个数据库, 代码如下所示：</p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><span style="color: #000000">db&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;openDatabase(</span><span style="color: #000000">"</span><span style="color: #000000">TestDb&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">1.0</span><span style="color: #000000">"</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">Client&nbsp;side&nbsp;storage&nbsp;test</span><span style="color: #000000">"</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">200000</span><span style="color: #000000">);<br />db.transaction(</span><span style="color: #0000ff">function</span><span style="color: #000000">(tx)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tx.executeSql(</span><span style="color: #000000">"</span><span style="color: #000000">SELECT&nbsp;id,&nbsp;text&nbsp;FROM&nbsp;Texts</span><span style="color: #000000">"</span><span style="color: #000000">, [],&nbsp;</span><span style="color: #0000ff">function</span><span style="color: #000000">(tx,&nbsp;result)&nbsp;{<br /></span><span style="color: #0000ff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for</span><span style="color: #000000">&nbsp;(</span><span style="color: #0000ff">var</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;i&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;result.rows.length;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)&nbsp;{<br /></span><span style="color: #0000ff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var</span><span style="color: #000000">&nbsp;row&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;result.rows.item(i);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; processText(&nbsp;row['id'],&nbsp;row['text']&nbsp;);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; },&nbsp; </span><span style="color: #0000ff">&nbsp; function</span><span style="color: #000000">(tx,&nbsp;error)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; alert('Failed&nbsp;to&nbsp;retrieve&nbsp;texts&nbsp;from&nbsp;the&nbsp;database&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">&nbsp;'&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;error.message);<br /></span><span style="color: #0000ff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp; });<br />});<br /></span></div>
<p>&nbsp;&nbsp;&nbsp;&nbsp; 使用QtWebkit，你可以使用QtSql模块访问同一个数据库。这在混合开发中是一个非常有用的特征。比如，你的应用中的web页面在和本地部分共享数据时，可以用同样的机制来保存数据。<br />&nbsp;&nbsp;&nbsp;&nbsp; 为了避免安全问题，客户端的数据库只能由具有正确权限的一方在JavaScript中访问。本地的C++代码也可以通过静态的<span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: 16px Simsun; white-space: normal; orphans: 2; letter-spacing: normal; color: #000000; word-spacing: 0px; font-size-adjust: none; font-stretch: normal" class="Apple-style-span"><span style="font-family: -webkit-monospace; font-size: 13px" class="Apple-style-span">QWebSecurityOrigin::allOrigins 方法，或者通过<span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: 16px Simsun; white-space: normal; orphans: 2; letter-spacing: normal; color: #000000; word-spacing: 0px; font-size-adjust: none; font-stretch: normal" class="Apple-style-span"><span style="font-family: -webkit-monospace; font-size: 13px" class="Apple-style-span">QWebFrame::securityOrigin <span style="font-size: small"><font size="2">来访问所有安全对象</font></span></span></span>。<br />&nbsp;&nbsp;&nbsp;</span></span><br class="Apple-interchange-newline" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;通过databases方法，可以访问一个QWebDatabaew方法列表，每一个web数据库对象有一个filename属性，可以用来在本地代码中访问数据库。</p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><span style="color: #000000">QWebDatabase&nbsp;webdb&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;mySecurityOrigin.databases()[index];<br />QSqlDatabase&nbsp;sqldb&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;QSqlDatabase::addDatabase(</span><span style="color: #000000">"</span><span style="color: #000000">QSQLITE</span><span style="color: #000000">"</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">webconnection</span><span style="color: #000000">"</span><span style="color: #000000">);<br />sqldb.setDatabaseName(webdb.fileName());<br /></span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(sqldb.open())&nbsp;{<br />&nbsp;&nbsp;&nbsp; QStringList&nbsp;tables&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;sqldb.tables();<br />&nbsp;&nbsp; <img alt="" src="http://www.cppblog.com/Images/dot.gif" /><br />}<br /></span></div>
<p><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在WEB和本地应用连接事件的能力，再加上共享数据的机制，更易于模糊WEB和桌面之间的界限。</p>
<p><br /><br />（三）WEB转换</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 很多通过WEB得到的数据并不适合直接显示。比如网络新闻, 地理数据，以及其它特定数据格式的应用。QT的网络模块可以以一种很简单的方式下载这样的数据，然后解析数据，把它转换成能被自己的代码处理的合适的格式。也可以通过QtXmlPatterns模块处理，当输出格式是XML或者想在XHMTL网页中显示时，这样更加方便。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 我们通过一个小例子来大致了解一下这个有意思的部分。我们下载一个新闻源，用XSLT把它从XML转换成XHTML，然后通过QWebPage把它显示出来。（图略）<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: 16px Simsun; white-space: normal; orphans: 2; letter-spacing: normal; color: #000000; word-spacing: 0px; font-size-adjust: none; font-stretch: normal" class="Apple-style-span"><span style="font-family: -webkit-monospace; font-size: 13px" class="Apple-style-span">QNetworkAccessManager 类让你很容易处理客户端和WEB服务器之间的交互。它帮你处理诸如代理，保存设置，COOKIE以及SSL会话这些细节。总之，它不但让你在象上传下载这些常见情况更加容易，还可以帮你处理登录，认证这些复杂会话。<br />&nbsp;&nbsp;&nbsp; 为了下载我们例子中的新闻源，我们所有要做的就是创建一个<span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: 16px Simsun; white-space: normal; orphans: 2; letter-spacing: normal; color: #000000; word-spacing: 0px; font-size-adjust: none; font-stretch: normal" class="Apple-style-span"><span style="font-family: -webkit-monospace; font-size: 13px" class="Apple-style-span">QNetworkAccessManager 然后调用它的get方法。结果通过<span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: 16px Simsun; white-space: normal; orphans: 2; letter-spacing: normal; color: #000000; word-spacing: 0px; font-size-adjust: none; font-stretch: normal" class="Apple-style-span"><span style="font-family: -webkit-monospace; font-size: 13px" class="Apple-style-span"> finished(QNetworkReply*) </span></span>信号返回。<br /></span></span></span></span></p>
<p><span style="color: #000000">{<br /><img alt="" src="http://www.cppblog.com/Images/dot.gif" /><br />QNetworkAccessManager&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">manager&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;QNetworkAccessManager(&nbsp;</span><span style="color: #0000ff">this</span><span style="color: #000000">&nbsp;);<br />connect(&nbsp;manager,&nbsp;SIGNAL(finished(QNetworkReply</span><span style="color: #000000">*</span><span style="color: #000000">)), </span><span style="color: #0000ff">this</span><span style="color: #000000">, SLOT(handleReply(QNetworkReply</span><span style="color: #000000">*</span><span style="color: #000000">))&nbsp;);<br />connect(&nbsp;manager,&nbsp;SIGNAL(finished(QNetworkReply</span><span style="color: #000000">*</span><span style="color: #000000">)), m_progressBar,&nbsp;SLOT(hide())&nbsp;);<br />QNetworkReply&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">reply&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;manager</span><span style="color: #000000">-&gt;</span><span style="color: #0000ff">get</span><span style="color: #000000">(&nbsp;QNetworkRequest(&nbsp;QUrl(&nbsp;feedUrl&nbsp;)&nbsp;)&nbsp;);<br />connect(&nbsp;reply,&nbsp;SIGNAL(downloadProgress(qint64,&nbsp;qint64)), </span><span style="color: #0000ff">this</span><span style="color: #000000">, SLOT(updateProgress(qint64,qint64))&nbsp;);<br />}</span></p><img src ="http://www.cppblog.com/boymaster/aggbug/194954.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/boymaster/" target="_blank">双鱼座的程序员</a> 2012-11-09 09:30 <a href="http://www.cppblog.com/boymaster/articles/194954.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Qt上截取中英文混合字符串的指定长度内容</title><link>http://www.cppblog.com/boymaster/articles/194020.html</link><dc:creator>双鱼座的程序员</dc:creator><author>双鱼座的程序员</author><pubDate>Mon, 29 Oct 2012 07:39:00 GMT</pubDate><guid>http://www.cppblog.com/boymaster/articles/194020.html</guid><wfw:comment>http://www.cppblog.com/boymaster/comments/194020.html</wfw:comment><comments>http://www.cppblog.com/boymaster/articles/194020.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/boymaster/comments/commentRss/194020.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/boymaster/services/trackbacks/194020.html</trackback:ping><description><![CDATA[如两个字符串：<br />QString str("中华人民共和国123中华人民共和国");<br />QString s = getString(str, 20);<br />要截取后，输出：<br />s&nbsp;= "中华人民共和国123中...";<br />即要把中英文混合的字符串，在截取长度要一致，而且不能出现中文被从中间截断的情况<br />下面直接贴代码：
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #0000ff">static</span><span style="color: #000000">&nbsp;QString&nbsp;getString(QString&nbsp;s,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;l)<br /><img id="Codehighlighter1_43_329_Open_Image" onclick="this.style.display='none'; Codehighlighter1_43_329_Open_Text.style.display='none'; Codehighlighter1_43_329_Closed_Image.style.display='inline'; Codehighlighter1_43_329_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_43_329_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_43_329_Closed_Text.style.display='none'; Codehighlighter1_43_329_Open_Image.style.display='inline'; Codehighlighter1_43_329_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_43_329_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_43_329_Open_Text"><span style="color: #000000">{&nbsp;&nbsp;&nbsp;&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;QString&nbsp;temp&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;s;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;QRegExp&nbsp;rx(</span><span style="color: #000000">"</span><span style="color: #000000">[\u4e00-\u9fa5]</span><span style="color: #000000">"</span><span style="color: #000000">);<br /><img id="Codehighlighter1_145_160_Open_Image" onclick="this.style.display='none'; Codehighlighter1_145_160_Open_Text.style.display='none'; Codehighlighter1_145_160_Closed_Image.style.display='inline'; Codehighlighter1_145_160_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_145_160_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_145_160_Closed_Text.style.display='none'; Codehighlighter1_145_160_Open_Image.style.display='inline'; Codehighlighter1_145_160_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(temp.replace(rx,&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">zz</span><span style="color: #000000">"</span><span style="color: #000000">).length()&nbsp;</span><span style="color: #000000">&lt;=</span><span style="color: #000000">&nbsp;l)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_145_160_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_145_160_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;s;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /><img id="Codehighlighter1_198_306_Open_Image" onclick="this.style.display='none'; Codehighlighter1_198_306_Open_Text.style.display='none'; Codehighlighter1_198_306_Closed_Image.style.display='inline'; Codehighlighter1_198_306_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_198_306_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_198_306_Closed_Text.style.display='none'; Codehighlighter1_198_306_Open_Image.style.display='inline'; Codehighlighter1_198_306_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">s.length();&nbsp;i</span><span style="color: #000000">&gt;=</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;i</span><span style="color: #000000">--</span><span style="color: #000000">)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_198_306_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_198_306_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;temp&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;s.mid(</span><span style="color: #000000">0</span><span style="color: #000000">,i);<br /><img id="Codehighlighter1_265_299_Open_Image" onclick="this.style.display='none'; Codehighlighter1_265_299_Open_Text.style.display='none'; Codehighlighter1_265_299_Closed_Image.style.display='inline'; Codehighlighter1_265_299_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_265_299_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_265_299_Closed_Text.style.display='none'; Codehighlighter1_265_299_Open_Image.style.display='inline'; Codehighlighter1_265_299_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(temp.replace(rx,&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">zz</span><span style="color: #000000">"</span><span style="color: #000000">).length()&nbsp;</span><span style="color: #000000">&lt;=</span><span style="color: #000000">&nbsp;l)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_265_299_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_265_299_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;s.mid(</span><span style="color: #000000">0</span><span style="color: #000000">,i)&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span style="color: #000000">"</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;QString(</span><span style="color: #000000">""</span><span style="color: #000000">);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span></div> <img src ="http://www.cppblog.com/boymaster/aggbug/194020.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/boymaster/" target="_blank">双鱼座的程序员</a> 2012-10-29 15:39 <a href="http://www.cppblog.com/boymaster/articles/194020.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>