




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
附录一:英文原文CreatingGraphics/ViewWindowsByreimplementingacustomQWidgetsubclassspaintEvent()andusingaQPainter,wecandrawanythingwewant.Thisisidealforcustomwidgets,butisnotatallconvenientifwewanttodrawlotsofindividualitems,especiallyifwewanttoprovidetheuserwiththeabilitytointeractwiththeitems.Forexample,inthepastsomeusershavecreatedgraphicalapplicationsusingliterallythousandsofcustomwidgetstostandasgraphicalitems,andalthoughwidgetpaintingisveryfast,handlingasinglemouseclickinsuchsituationscouldeasilyconsumealmostthewholeCPUsprocessingcapability.Fortunately,Qt4.2introducedthegraphics/viewarchitecturewhichperfectlyfulfillstheneedforhigh-performanceitem-baseddrawingandinteraction.AlthoughoriginallydesignedasasuperiorreplacementforQt3sQCanvasclass,Qt4sgraphics/viewarchitecturehasgonefarbeyondthecanvassfunctionality.Infact,someapplicationsnowuseaQGraphicsViewastheirmainwindowscentralwidgetandplaceallthewidgetsusedtoprovidetheuserinterfaceinsidetheviewasgraphicsitemsintheirownright.Inthischaptersfirstsectionwebeginwithabriefoverviewofthegraphics/viewarchitecture,includingasidebaronsomesignificantchangesintroducedinQt4.6.Then,inthesecondsection,wewillreviewanapplicationwhosemainwindowscentralwidgetisaQGraphicsViewandwhichhasbothwidgetitemsandconventionalgraphicsitems.Andfinally,inthechaptersthirdsection,wewilllookatasimpleQGraphicsItemsubclassandtheQGraphicsItemAPI.Inthenextchapterwewilllookatamoreconventionalgraphics/viewapplicationabasicdrawingprogramandtherewewilllookmorecloselyatmostofthegraphics/viewclasses,andwillpresentmoreexamplesofhowtocreatecustomgraphicsitems.Incidentally,wewillrevisittheexamplespresentedinthischapterandthenextinChapter13wherewecreatemodifiedversionsofthemthatmakeuseofsomeQt4.6-specificfeatures.RatherlikeQtsmodel/viewarchitecture,thegraphics/viewarchitecturehasanon-visualclassforholdingtheitemdataasamodel(QGraphicsScene),andaclassforvisualizingthedata(QGraphicsView).Wecanvisualizethesamesceneinmanydifferentviewsifthatisrequired.AgraphicsscenecontainsitemsthatarederivedfromtheabstractQGraphicsItemclass.Sinceitsoriginalintroduction,Qtsgraphics/viewarchitecturehasbenefitedfromagreatdealofdevelopmentefforttoimprovebothitsspeedanditscapabilities.Scenescanbescaled,rotated,andprinted,andrenderingcanbedoneusingQtsrenderingengineorusingOpenGL.Thearchitecturealsosupportsanimationanddraganddrop.Graphicsscenescanbeusedtopresentanythingfromjustafewitemsuptotensofthousandsofitemsormore.Qtprovidesmanypredefinedgraphicsitemtypesthatcanbeusedoutofthebox;theyareshowninFigure11.1.Mostoftheclassnamesareself-explanatory,butwewillmentionafewthatmaynotbesoobvious.AQGraphicsPathItemrepresentsaQPainterPathessentiallyanarbitraryshapethatiscomposedofallthefundamentalthingsthatQtcandraw,including,arcs,Bziercurves,chords,ellipses,lines,rectangles,andtext.AQGraphicsSimpleTextItemrepresentsapieceofplaintextandaQGraphicsTextItemrepresentsapieceofQtrichtext(whichcanbespecifiedusingHTML;wediscussedrichtextintheprevioustwochapters).TheQGraphicsWidgetclassisprovidedasabaseclassforthecreationofcustomwidgetsthataredesignedtoliveingraphicsscenes.ItisalsopossibletoembedstandardQWidget-derivedwidgetsinscenesthisisdonebyaddingthewidgettoaQGraphicsProxyWidgetandputtingtheproxywidgetintothescene.Usingproxywidgets(orQWidgetsdirectly)is“slow”butwhetherthisisnoticeablewilldependontheapplication.TheQGraphicsWebViewclasswasintroducedinQt4.6andprovidesagraphicsitemversionoftheQWebViewclasswediscussedinChapter1,forpresentingwebcontentinascene.ForscenesthathavesmallnumbersofitemswecanusetheQGraphicsObjectsintroducedinQt4.6orforQt4.5andearlier,wecanderivefrombothQObjectandQGraphicsItemasthebasisforourowncustomitems.Thisincreaseseachitemsoverhead(e.g.,itemsconsumemorememory),butprovidestheconvenienceofsupportforsignalsandslotsandforQtspropertysystem.ForscenesthathavelotsandlotsofitemsitisusuallybesttousethelightweightQGraphicsItemclassasthebasisforthecustomitemsthatwillappearinlargenumbers,andonlyuseQGraphicsObjectsforitemsthatwillappearinsmallnumbers.Thegraphicsviewclassesareessentiallytwo-dimensional,althougheveryitemhasazvalue,withhigherz-valueditemsbeingdrawninfrontofthosewithlowerzvalues.Collisiondetectionisbasedonitem(x,y)positions.Inadditiontoinformationaboutcollisions,thescenecantelluswhichitemscontainaparticularpointorareinaparticularregion,andwhichareselected.Scenesalsohaveaforegroundlayerwhichisuseful,forexample,todrawagridthatoverlaysalltheitemsinthescene;theyalsohaveabackgroundlayerthatisdrawnunderneathalltheitems,usefulforprovidingabackgroundimageorcolor.Itemsareeitherchildrenofthescene,orachildofanotheritem,ratherlikeQtsnormalparentchildwidgetrelationships.Whentransformationsareappliedtoanitem,theyareautomaticallyappliedtoalltheitemschildren,recursivelytothegreatestdescendant.Thismeansthatifanitemismovedforexample,draggedbytheuserallofitschildren(andtheirchildren,andsoon)willbedraggedwithit.ItispossibletomakeachilditemignoreitsparentstransformationsbycallingQGraphicsItem:setFlag(QGraphicsItem:ItemIgnoresTransformations).Other,morecommonlyusedflagsincludeonesformakinganitemmovable,selectable,andfocusablebytheuser.(AlltheflagsarelistedinTables11.6and11.7;.)ItemscanalsobegroupedbymakingthemchildrenofaQGraphicsItemGroup;thisisusefulforcreatingadhoccollectionsofitems.Thegraphicsviewclassesusethreedifferentcoordinatesystems,althoughinpracticeweusuallycareaboutonlytwoofthem.Viewsusethephysicalcoordinatesystem.ScenesusealogicalcoordinatesystemthatwenormallydefinebypassingaQRectFtotheirconstructor.Qtautomaticallymapsscenecoordinatestoviewcoordinates.Inessence,scenesuse“window”(logical)coordinatesandviewsuse“viewport”(physical)coordinates.So,whenwearepositioningitemsweplacethemintermsofscenecoordinates.Thethirdcoordinatesystemistheoneusedbyitems.Thisisparticularlyconvenientbecauseitisalogicalcoordinatesystemcenteredonpoint(0,0).Eachitems(0,0)isactuallyattheitemscenterpositioninthescene(apartfromtextitemswhereitistheitemstop-leftcorner).Thismeansthatinpractice,wecanalwaysdrawitemsintermsoftheirowncenterpointandwedonothavetocareaboutanytransformationsthathavebeenappliedtothembyparentitems,sincethescenewillautomaticallytakecareoftheseforus.NotealsothatinQt,y-coordinatesincreasedownwardforexample,point(5,8)is6pixelsabovepoint(5,14).Figureillustratestherelationshipbetweensceneanditemcoordinates.CertainaspectsofQtsgraphics/viewarchitecturesbehaviorhavechangedbetweenQt4.5andQt4.6;thesearesummarizedinthe“Qt4.6Graphics/ViewBehaviorChanges”sidebar.InthissectionwewillreviewthePetriDishapplication(petridish1)showninFigureTheapplicationhasaMainWindowclassthatinheritsQMainWindowandusesaQGraphicsViewasitscentralwidget.PetriDishisadialog-stylesimulationapplicationthatsimulates“cells”thatgrowiftheyarenttoocrowded,butshrinkiftheyaretooisolated,toocrowded,ortoobig,andwheresmallcellsrandomly“die”.Wewontsaymuchmoreaboutthesimulationitself,orabouttheapplicationslogic,sincethefocusofthischapterisQtsgraphics/viewarchitecture.Herewewillreviewtherelevantmainwindowmethods(orextractsfromthesemethods)toshowhowtocreateamainwindowbasedonagraphicsscene.AndinthenextsectionwewilllookattheCellitems(derivedfromQGraphicsItem),focusingonthebasicsofcreatingacustomgraphicsitemandintroducingtheQGraphicsItemAPIwithoutlookingattheirrelevantsimulationlogic.(Thesourcecodeisinthepetridish1subdirectory.)Theapplicationhasstart,pause/resume,stop,andquitbuttonstocontrolthesimulation,andtheusercansetaninitialcellcountandwhethertoshowcellIDswhichisusefulforcellsthatareotherwisetoosmalltosee.(Theinitialcellcountisdisabledduringasimulationrun,asthescreenshotillustrates.)TheuserinterfaceusesacoupleofQLCDNumberstoshowhowmanycellsareleftandhowmanyiterationsthesimulationhasrunfor.Wewillbeginbylookingatthemainwindowsconstructor,andthenatsomeofthehelpermethodsthatarerelevanttothegraphics/viewprogrammingweareconcernedwithhere.MainWindow:MainWindow(QWidget*parent):QMainWindow(parent),simulationState(Stopped),iterations(0)scene=newQGraphicsScene(this);scene-setItemIndexMethod(QGraphicsScene:NoIndex);createWidgets();createProxyWidgets();createLayout();createCentralWidget();createConnections();startButton-setFocus();setWindowTitle(QApplication:applicationName();ThecreationoftheQGraphicsSceneisslightlyunusualsincewehavenotspecifiedthescenesdimensions.Althoughweknowtheheightweneed(highenoughforthepetridishplussomemargin),thewidthdependsonhowwidethewidgetsare,sowewillsetthedimensionsoncethewidgetshavebeencreatedandlaidout.Whenitemsareadded,moved,orremovedfromascene,locationcomputationsarerequired.Forexample,ifanitemisaddedinavisibleportionofthesceneitmustbedrawn,orifavisibleitemismovedorremoved,whateverithadcoveredandisnowrevealedmustbedrawn.ForsceneswithlotsofstaticitemsthesecomputationscanbeconsiderablyspeededupbyusinganindexmethodofQGraphicsScene:BspTreeIndex(BinarySpacePartitioning);butforhighlydynamicsceneswithlotsofitemsadded,moved,orremoved,itisbettertoswitchoffindexing(aswedohere),sincetheoverheadofusingitoutweighsthesavingsitdelivers.附录二:中文译文创建图形/视图窗口通过自定义QWidget源派生类,实现paintEvent()并使用QPainter,我们可以随心所欲地绘制任何需要的内容,这种方法对于自定义的窗口部件(widget)很理想,但如果要绘制大量独立的项(item)就不怎么方便了,尤其是当想要向用户提供与图形项的图像应用,虽然窗口部件的绘制非常快,单在这种情况下处理单个鼠标点击轻易就消耗了几乎所有的CPU处理能力。型号QT4引入了图形/视图架构,完美的满足了基于项的高性能绘制和交互的需求。QT4的图形视图架构原本的设计是取代并超越QT3的QCanvas类,但它已经远远炒股了画布的功能。实际应用中有些应用程序用QGraphicsView作为主窗口的中心窗体,把所有提供用户界面的窗口部件作为图形项放在视图中。在11.1节我们先从图形驶入架构的概览开始,涵盖一些QT4中新加入的关键变化。在11.2节将查看一个用QFraphicsView作为主窗口的中心窗体的应用程序,QGraphicsView中既包含窗口部件项又包含传统的图形项。最后在11.3节我们将介绍一个简单的QGriphicsItem的子类和QGriphicsItem的API。下一张将介绍一个更传统的图形视图应用程序(一个基本的绘图程序)更深入的了解大部分图形/视图类,还会介绍更多关于如何创建自定义图形项的例子。顺便提一句,这一章里出现的例子将在第13章中使用一些QT4中才有的特性创建一个修改版。与QT的墨香视图架构十分相似,图形视图架构有一个作为模型的不可见类(QGraphicsScene)容纳项的数据,同事用另一个(QGraphicsView)来可视化显示数据。如有需要,我们可以在多个不用的视图内可视化显示同一场景。图形场景包含了从抽象类QGraphicsItem派生类而来的项。从最初被引入到QT中至今,得益于大量的开发努力,图形视图架构在速度和功能上都有了很大的进步。场景可缩放、旋转并可打印,渲染则可以使用QT的渲染引擎或者使用OPENGL。该架构还支持动画和拖放操作。图形场景可以用来呈现任何内容,从几个项直至成千上万乃至更多的项。QT提供了可以直接使用的众多预定义图形项类型,如图11.1所示。多数类的名称可顾名思义,单我们会体积一些不那么显而易见的类。QGraphicsPathItem代表一个QPainterPath,本质上是一个任意的形状,由所有基本的QT可绘制的物体,包括户县、贝吉爱曲线、弦、椭圆形、线段、矩形和文本一一组合而成。QGraphicsWidget类是创建在图形场景中存在的自定义窗口部件的积累。通过把窗口部件添加到QGraphicsProxyWidget,再把代理窗口部件(或者直接使用窗口部件)比较慢,单至于会不会慢到被质疑道的成都还要看应用程序。Qt4引入了QGraphicsWebView类,提供QWebView类(我们在第一章中讨论了这个类)的图形项版本,用于在场景中呈现网络内容(Webcontent).对于项不多的场景,可以使用在Qt4中引入的QGraphicsObjects类,对于Qt4及更早的版本,可以吧QObject和QGraphicsItem两个雷作为基础派生自定义的项。这会增加每个项的开销(如项会占用更多的内存),单可以提供对信号和槽以及Qt的属性系统的支持。对于有很多很多项的场景来说,通常最好使用轻量级的QGraphicsItem类作为数目众多的自定义项的基础,仅对数目较少的项使用QgraphicsObjects。虽然图形视图类的每个项都有一个z值,z值较大的项会被绘制在z值较小的项之上,但图形视图类本质上是二维的。冲突检测基于项的(x,y)坐标位置。除了冲突信息之外,场景还可以告诉我们哪些项包含一个特定的点,哪些项在某特定领域中以及哪些项被选中。场景还有一个有用的前景层,例如在场景中绘制一个覆盖所有项的网络:场景还有一个背景层在所有项之下绘制,可用于提供背景图或者背景色。项可以是场景或者另一个项的子对象,和Qt窗口部件的父子关系十分相似。当一个项应用了变换,该项的所有子对象自动应用该变换,地柜应用至最深层次的子对象。这意味着如果一个项被移动(如被永辉拖动),它的所有子项(及子项的子孙)将和它同事被拖动。通过调用QGraphicsItem:setFlag(QGraphicsItem:ItemIgnoresTransformations)可以让子项忽略父项的变换。除此以外更常用的标志包括设置项可移动、选中和获得焦点的标志。通过吧项设置为QGraphicsItemGroup的子项,可以吧它们分组;这对于创建激动的项集合很有用。图形视图类使用的三种不同的坐标系统,虽然在实际的应用中通常只关心其中的两种。视图使用坐标系统,场景使用一朝在构造函数中传入的QRectF定义的逻辑坐标系统。QT自动进行从场景坐标到视图坐标的映射。重点在于场景用窗口(window)的(逻辑)坐标而视图用视口的屋里坐标。所以我们放置项是用的是场景的坐标系统。第三个坐标系统是项使用的坐标系统,该系统非常使用,应为它是中心在(0,0)点的逻辑坐标系统。每个项的(0,0)点实际上在该项场景中所在位置的中心
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 园区租赁合同书
- 浙江省金华市金东区2024-2025学年七年级上学期语文期中考试试卷(含答案)
- 透析护理进修汇报
- 警校生裸考题目及答案解析
- 精神控制考试题目及答案
- 经典选聘面试题目及答案
- 医疗与医药行业:医疗器械注册与临床试验法规解读与应用
- 数字化背景下商业地产项目运营管理与客户体验优化的策略与实施报告
- 医美行业监管趋势下2025年激光美容技术的创新与发展报告
- 教育信息化2.0时代2025年教师信息技术与教育教学改革案例分析策略报告
- 生物强化技术在农业废弃物堆肥处理中的应用及研究进展
- 厌氧罐负压失稳处理方案
- 麻风病防治知识竞赛题库填空题
- 前列腺病人的心理护理
- 互联网护理管理
- 骨科降低卧床患者便秘发生率医院护理质量QCC改善案例
- 《业务员基本素质》课件
- 物联网工程专业介绍
- 成人高考成考英语(高起专)试题及答案指导(2025年)
- 血液透析中低血压的预防及处理
- (高清稿)DB44∕T 2515-2024 水利工程水文化设计导则
评论
0/150
提交评论