Qt WebKit中英文翻译文档格式.docx
- 文档编号:19961854
- 上传时间:2023-01-12
- 格式:DOCX
- 页数:22
- 大小:32.75KB
Qt WebKit中英文翻译文档格式.docx
《Qt WebKit中英文翻译文档格式.docx》由会员分享,可在线阅读,更多相关《Qt WebKit中英文翻译文档格式.docx(22页珍藏版)》请在冰豆网上搜索。
Intro<
/span>
Snippets<
/p>
p>
<
Content<
Here<
*/
QWebElementCollectionallSpans=document.findAll("
span"
);
QWebElementCollectionintroSpans=document.findAll("
p.introspan"
Thefirstlistcontainsallspanelementsinthedocument.Thesecondlistcontainsspanelementsthatarechildrenofp,classifiedwithintro.
UsingfindFirst()ismoreefficientthancallingfindAll(),andextractingthefirstelementonlyinthelistreturned.
AlternativelyyoucantraversethedocumentmanuallyusingfirstChild()andnextSibling():
frame->
setHtml("
html>
body>
FirstParagraph<
SecondParagraph<
/body>
/html>
"
QWebElementdoc=frame->
QWebElementbody=doc.firstChild();
QWebElementfirstParagraph=body.firstChild();
QWebElementsecondParagraph=firstParagraph.nextSibling();
Individualelementscanbeinspectedorchangedusingmethodssuchasattribute()orsetAttribute().Forexamle,tocapturetheuser'
sinputinatextfieldforlateruse(auto-completion),abrowsercoulddosomethinglikethis:
QWebElementfirstTextInput=document.findFirst("
input[type=text]"
QStringstoredText=firstTextInput.attribute("
value"
Whenthesamepageislaterrevisited,thebrowsercanfillinthetextfieldautomaticallybymodifyingthevalueattributeoftheinputelement:
textInput.setAttribute("
storedText);
Anotherusecaseistoemulateaclickeventonanelement.ThefollowingcodesnippetdemonstrateshowtocalltheJavaScriptDOMmethodclick()ofasubmitbutton:
/*Assumethatthedocumenthasthefollowingstructure:
<
formname="
myform"
action="
submit_form.asp"
method="
get"
>
inputtype="
text"
name="
myfield"
submit"
value="
Submit"
/form>
*/
QWebElementbutton=document.findFirst("
input[type=submit]"
button.evaluateJavaScript("
click()"
TheunderlyingcontentofQWebElementisexplicitlyshared.CreatingacopyofaQWebElementdoesnotcreateacopyofthecontent.Instead,bothinstancespointtothesameelement.
ThecontentsofchildelementscanbeconvertedtoplaintextwithtoPlainText();
toXHTMLusingtoInnerXml().Toincludetheelement'
stagintheoutput,usetoOuterXml().
ItispossibletoreplacethecontentsofchildelementsusingsetPlainText()andsetInnerXml().Toreplacetheelementitselfanditscontents,usesetOuterXml().
2.QtWebKit
Overview
Thetechnology
TheQtWebKitbridgeisamechanismthatextendsWebKit'
sJavaScriptenvironmenttoaccessnativeobjectsthatarerepresentedasQObjects.IttakesadvantageoftheQObjectintrospection,apartoftheObjectModel,whichmakesiteasytointegratewiththedynamicJavaScriptenvironment,forexampleQObjectpropertiesmapdirectlytoJavaScriptproperties.
Forexample,bothJavaScriptandQObjectshaveproperties:
aconstructthatrepresentagetter/setterpairunderonename.
UseCases
TherearetwomainusecasesfortheQtWebKitbridge.Webcontentinanativeapplication,andThinClients.
WebContentinaNativeApplication
ThisisacommonusecaseinclassicQtapplication,andadesignpatternusedbyseveralmodernapplications.Forexample,anapplicationthatcontainsamedia-player,playlistmanager,andmusicstore.Theplaylistmanagerisusuallybestauthoredasaclassicdesktopapplication,withthenative-lookingrobustQWidgetshelpingwithproducingthatapplication.Themedia-playercontrol,whichusuallylookscustom,canbewrittenusingtheGraphicsViewframeworkorwithinadeclarativewaywithQtDeclarative.Themusicstore,whichshowsdynamiccontentfromtheinternetandgetsmodifiedrapidly,isbestauthoredinHTMLandmaintainedontheserver.
WiththeQtWebKitbridge,thatmusicstorecomponentcaninteractwithnativepartsoftheapplication,forexample,ifafileneedstobesavedtoaspecificlocation.
ThinClient
AnotherusecaseisusingQtasanativebackendtoafullwebapplication,referredtohereasathinclient.Inthisuse-case,theentireUIisdrivenbyHTML,JavaScriptandCSS,andnativeQt-basedcomponentsareusedtoallowthatapplicationaccesstonativefeaturesnotusuallyexposedtotheweb,ortoenablehelpercomponentsthatarebestwrittenwithC++.
AnexampleforsuchaclientisaUIforavideo-on-demandserviceonaTV.TheentirecontentandUIcanbekeptontheserver,serveddynamicallythroughHTTPandrenderedwithWebKit,withadditionalnativecomponentsforaccessinghardware-specificfeatureslikeextractingthelistofimagesoutofthevideo.
DifferencefromOtherBridgeTechnologies
OfcourseQtWebKitisnottheonlybridgetechnologyoutthere.NPAPI,forexample,isalong-timestandardorweb-nativebridging.DuetoQt'
smeta-objectsystem,fullapplicationsbuiltpartiallywithweb-technologiesaremucheasiertodevelop.NPAPI,however,ismoregearedtowardscross-browserplugins,duetoitbeinganacceptedstandard.
Whendevelopingapluginforabrowser,NPAPIisrecommended.WhendevelopingafullapplicationthatutilizesHTML-rendering,theQtWebKitbridgeisrecommended.
AccessingQObjects
CreatingthelinkviaQWebFrame
Bydefault,noQObjectsareaccessiblethroughthewebenvironment,forsecurityreasons.ToenablewebcontentaccessforanativeQObject,theapplicationmustexplicitlygrantitaccess,usingthefollowingcall:
//...
QWebFrame*frame=myWebPage->
mainFrame();
frame->
addToJavaScriptWindowObject("
someNameForMyObject"
myObject);
SeeQWebFrame:
addToJavaScriptWindowObject()formoreinformation.
UsingSignalsandSlots
TheQtWebKitbridgeadaptsQt'
scentralSignalsandSlotsfeatureforscripting.TherearethreeprincipalwaystousesignalsandslotswiththeQtWebKitbridge:
HybridC++/script:
C++applicationcodeconnectsasignaltoascriptfunction.Forexample,thescriptfunctioncanbeafunctionthattheuserhastypedin,oronethatyouhavereadfromafile.ThisapproachisusefulifyouhaveaQObjectbutdon'
twanttoexposetheobjectitselftothescriptingenvironment;
youjustwantascripttobeabletodefinehowasignalshouldbereactedto,andleaveituptotheC++sideofyourapplicationtoestablishtheconnection.
Hybridscript/C++:
Ascriptcanconnectsignalsandslotstoestablishconnectionsbetweenpre-definedobjectsthattheapplicationexposestothescriptingenvironment.Inthisscenario,theslotsthemselvesarestillwritteninC++,butthedefinitionoftheconnectionsisfullydynamic(script-defined).
Purelyscript-defined:
Ascriptcanbothdefinesignalhandlerfunctions(effectively"
slotswritteninJavaScript"
),andsetuptheconnectionsthatutilizethosehandlers.Forexample,ascriptcandefineafunctionthatwillhandletheQLineEdit:
returnPressed()signal,andthenconnectthatsignaltothescriptfunction.
NotethatQtScriptfunctionssuchasqScriptConnectareunavilableinthewebenvironment.
SignaltoFunctionConnections
connect(function);
Inthisformofconnection,theargumenttoconnect()isthefunctiontoconnecttothesignal.
functionmyInterestingScriptFunction(){...}
...
myQObject.somethingChanged.connect(myInterestingScriptFunction);
TheargumentcanbeaJavaScriptfunction,asintheaboveexample,oritcanbeaQObjectslot,asinthefollowingexample:
myQObject.somethingChanged.connect(myOtherQObject.doSomething);
WhentheargumentisaQObjectslot,theargumenttypesofthesignalandslotdonotnecessarilyhavetobecompatible;
Ifnecessary,theQtWebKitbridgewill,performconversionofthesignalargumentstomatchtheargumenttypesoftheslot.
Todisconnectfromasignal,youinvokethesignal'
sdisconnect()function,passingthefunctiontodisconnectasargument:
myQObject.somethingChanged.disconnect(myInterestingFunction);
myQObject.somethingChanged.disconnect(myOtherQObject.doSomething);
Whenascriptfunctionisinvokedinresponsetoasignal,thethisobjectwillbetheGlobalObject.
SignaltoMemberFunctionConnections
connect(thisObject,function)
Inthisformoftheconnect()function,thefirstargumentistheobjectthatwillbeboundtothevariable,this,whenthefunctionspecifiedusingthesecondargumentisinvoked.
Ifyouhaveapushbuttoninaform,youtypicallywanttodosomethinginvolvingtheforminresponsetothebutton'
sclickedsignal;
passingtheformasthethisobjectmakessenseinsuchacase.
varobj={x:
123};
varfun=function(){print(this.x);
};
myQObject.somethingChanged.connect(obj,fun);
Todisconnectfromthesignal,passthesameargumentstodisconnect():
myQObject.somethingChanged.disconnect(obj,fun);
SignaltoNamedMemberFunctionConnections
connect(thisObject,functionName)
Thisformoftheconnect()functionrequiresthatthefirstargumentistheobjectthatwillbeboundtothevariablethiswhenafunctionisinvokedinresponsetothesignal.Thesecondargumentspecifiesthenameofafunctionthatisconnectedtothesignal,andthisreferstoamemberfunctionoftheobject
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Qt WebKit中英文翻译 WebKit 中英文 翻译
![提示](https://static.bdocx.com/images/bang_tan.gif)