qt drawing examples

Upload: sravanthivelagapudi

Post on 13-Apr-2018

250 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/27/2019 Qt Drawing Examples

    1/31

    /******************************************************************************** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).** Contact http//!!!."t-pro#ect.org/legal**** $his %ile is part o% the e&a'ples o% the t $oolit.

    **** $+,+C,D** ou 'ay use this %ile under the ter's o% the ,D license as %ollo!s**** 45edistribution and use in source and binary %or's6 !ith or !ithout** 'odi%ication6 are per'itted pro7ided that the %ollo!ing conditions are** 'et** * 5edistributions o% source code 'ust retain the abo7e copyright** notice6 this list o% conditions and the %ollo!ing disclai'er.** * 5edistributions in binary %or' 'ust reproduce the abo7e copyright** notice6 this list o% conditions and the %ollo!ing disclai'er in** the docu'entation and/or other 'aterials pro7ided !ith the

    ** distribution.** * either the na'e o% Digia Plc and its ubsidiary(-ies) nor the na'es** o% its contributors 'ay be used to endorse or pro'ote products deri7ed** %ro' this so%t!are !ithout speci%ic prior !ritten per'ission.****** $8 9:$;

  • 7/27/2019 Qt Drawing Examples

    2/31

    Binclude ,rushBinclude PenBinclude Pi&'apBinclude ;idget

    class 5ender

  • 7/27/2019 Qt Drawing Examples

    3/31

    ** 45edistribution and use in source and binary %or's6 !ith or !ithout** 'odi%ication6 are per'itted pro7ided that the %ollo!ing conditions are** 'et** * 5edistributions o% source code 'ust retain the abo7e copyright** notice6 this list o% conditions and the %ollo!ing disclai'er.** * 5edistributions in binary %or' 'ust reproduce the abo7e copyright

    ** notice6 this list o% conditions and the %ollo!ing disclai'er in** the docu'entation and/or other 'aterials pro7ided !ith the** distribution.** * either the na'e o% Digia Plc and its ubsidiary(-ies) nor the na'es** o% its contributors 'ay be used to endorse or pro'ote products deri7ed** %ro' this so%t!are !ithout speci%ic prior !ritten per'ission.****** $8 9:$;

  • 7/27/2019 Qt Drawing Examples

    4/31

    set,acground5ole(Palette,ase)A set

  • 7/27/2019 Qt Drawing Examples

    5/31

    Point(O06 0) GA

    5ect rect(106 206 N06 Q0)A

    PainterPath pathA path.'o7e$o(206 N0)A path.line$o(206 30)A path.cubic$o(N06 06 R06 R06 N06 N0)A

    int start

  • 7/27/2019 Qt Drawing Examples

    6/31

    painter.dra!llipse(rect)A breaA case

  • 7/27/2019 Qt Drawing Examples

    7/31

    ** %ro' this so%t!are !ithout speci%ic prior !ritten per'ission.****** $8 9:$;

  • 7/27/2019 Qt Drawing Examples

    8/31

    pri7ate 5ender

  • 7/27/2019 Qt Drawing Examples

    9/31

    C9P58$** 9;5 95 C9$5,>$95 ,

  • 7/27/2019 Qt Drawing Examples

    10/31

    de%ined(+;+@>

  • 7/27/2019 Qt Drawing Examples

    11/31

    brushtyleCo'bo,o&-addte'(tr(4Diagonal Cross4)6 tDiagCrossPattern)A brushtyleCo'bo,o&-addte'(tr(4Dense 14)6 tDense1Pattern)A brushtyleCo'bo,o&-addte'(tr(4Dense 24)6 tDense2Pattern)A brushtyleCo'bo,o&-addte'(tr(4Dense 34)6 tDense3Pattern)A brushtyleCo'bo,o&-addte'(tr(4Dense K4)6 tDenseKPattern)A brushtyleCo'bo,o&-addte'(tr(4Dense R4)6 tDenseRPattern)A

    brushtyleCo'bo,o&-addte'(tr(4Dense Q4)6 tDenseQPattern)A brushtyleCo'bo,o&-addte'(tr(4Dense 4)6 tDensePattern)A brushtyleCo'bo,o&-addte'(tr(4one4)6 to,rush)A

    brushtyleabel H ne! abel(tr(4J,rush4))A brushtyleabel-set,uddy(brushtyleCo'bo,o&)A

    other9ptionsabel H ne! abel(tr(49ptions4))A antialiasingChec,o& H ne! Chec,o&(tr(4J

  • 7/27/2019 Qt Drawing Examples

    12/31

    'ainayout-add;idget(penFoinabel6 26 26 t

  • 7/27/2019 Qt Drawing Examples

    13/31

    radialradient.setColor

  • 7/27/2019 Qt Drawing Examples

    14/31

    ** PC,$$>$ 99D 95 5=CA 99: >6** DP$9) 89;=5 CD

  • 7/27/2019 Qt Drawing Examples

    15/31

    signalspublic slotsGA

    Bendi% // @;D$+8'y!idget.cpp

    Binclude 4'y!idget.h47oid @y;idgetpaint7ent(Paint7ent *e7ent)E //create a Painter and pass a pointer to the de7ice. //< paint de7ice can be a ;idget6 a Pi&'ap or a 'age Painter painter(this)A

    //a si'ple line

    painter.dra!ine(16161006100)A

    //create a blac pen that has solid line //and the !idth is 2. Pen 'yPen(tblac6 26 tolidine)A painter.setPen('yPen)A painter.dra!ine(1006100610061)A

    //dra! a point 'yPen.setColor(tred)A painter.dra!Point(1106110)A

    //dra! a polygon Polygon polygonA polygon Point(1306 1K0) Point(1N06 10) Point(1N06 1K0) Point(2206 110) Point(1K06 100)A painter.dra!Polygon(polygon)A

    //dra! an ellipse //$he set5ender8int() call enables antialiasing6 telling Painter to use di%%erent //color intensities on the edges to reduce the 7isual distortion that nor'ally //occurs !hen the edges o% a shape are con7erted into pi&els painter.set5ender8int(Painter

  • 7/27/2019 Qt Drawing Examples

    16/31

    int 'ain( int argc6 char **arg7 )E

  • 7/27/2019 Qt Drawing Examples

    17/31

    Painter painter(this)A

    painter.setPen(tdarreen)Apainter.dra!5ect(16 26 Q6 K)APainter painter(this)A

    painter.setPen(tdarreen)Apainter.dra!ine(26 6 Q6 1)A;hen rendering !ith a pen !ith an e7en nu'ber o% pi&els6 the pi&els !ill be renderedsy'etrically around the 'athe'atical de%ined points6 !hile rendering !ith a pen !ith anodd nu'ber o% pi&els6 the spare pi&el !ill be rendered to the right and belo! the'athe'atical point as in the one pi&el case. ee the 5ect: diagra's belo! %orconcrete e&a'ples.5ect:

    ogical representation 9ne pi&el !ide pen

    $!o pi&el !ide pen $hree pi&el !ide pen

    ote that %or historical reasons the return 7alue o% the 5ectright() and 5ectbotto'()%unctions de7iate %ro' the true botto'-right corner o% the rectangle.5ectVs right() %unction returns le%t() S !idth() - 1 and the botto'() %unction returns top()S height() - 1. $he botto'-right green point in the diagra's sho!s the return coordinateso% these %unctions.;e reco''end that you si'ply use 5ect: instead $he 5ect: class de%ines arectangle in the plane using %loating point coordinates %or accuracy (5ect uses integercoordinates)6 and the 5ect:right() and 5ect:botto'() %unctions do return the truebotto'-right corner.

  • 7/27/2019 Qt Drawing Examples

    18/31

    nop rotate()scale() translate()ou can also t!ist the coordinate syste' around the origin using the Paintershear()%unction. ee the

  • 7/27/2019 Qt Drawing Examples

    19/31

    $his !ill gi7e us a 200 & 200 s"uare area6 !ith the origin (06 0) in the center6 that !e candra! on. ;hat !e dra! !ill sho! up in the largest possible s"uare that !ill %it in the

    !idget.ee also the ;indo!-=ie!port Con7ersion section. painter.sa7e()A painter.rotate(30.0 * ((ti'e.hour() S ti'e.'inute() / Q0.0)))A

    painter.dra!Con7e&Polygon(hour8and6 3)A painter.restore()A;e dra! the clocVs hour hand by rotating the coordinate syste' and callingPainterdra!Con7e&Polygon(). $hanVs to the rotation6 itVs dra!n pointed in the rightdirection.$he polygon is speci%ied as an array o% alternating &6 y 7alues6 stored in the hour8andstatic 7ariable (de%ined at the beginning o% the %unction)6 !hich corresponds to the %ourpoints (26 0)6 (06 2)6 (-26 0)6 and (06 -2R).$he calls to Paintersa7e() and Painterrestore() surrounding the code guaranteesthat the code that %ollo!s !onVt be disturbed by the trans%or'ations !eV7e used. painter.sa7e()A painter.rotate(Q.0 * (ti'e.'inute() S ti'e.second() / Q0.0))A

    painter.dra!Con7e&Polygon('inute8and6 3)A painter.restore()A;e do the sa'e %or the clocVs 'inute hand6 !hich is de%ined by the %our points (16 0)6(06 1)6 (-16 0)6 and (06 -K0). $hese coordinates speci%y a hand that is thinner and longerthan the 'inute hand. %or (int # H 0A # Q0A SS#) E i% ((# W R) XH 0) painter.dra!ine(O26 06 OQ6 0)A painter.rotate(Q.0)A G:inally6 !e dra! the cloc %ace6 !hich consists o% t!el7e short lines at 30-degreeinter7als.

  • 7/27/2019 Qt Drawing Examples

    20/31

    Painter painter(this)Apainter.set;indo!(5ect(-R06 -R06 1006 100))Ao!6 the logical coordinates (-R06-R0) correspond to the paint de7iceVs physicalcoordinates (06 0). ndependent o% the paint de7ice6 your painting code !ill al!aysoperate on the speci%ied logical coordinates.,y setting the 4!indo!4 or 7ie!port rectangle6 you per%or' a linear trans%or'ation o% the

    coordinates. ote that each corner o% the 4!indo!4 'aps to the corresponding corner o%the 7ie!port6 and 7ice 7ersa. :or that reason it nor'ally is a good idea to let the 7ie!portand 4!indo!4 'aintain the sa'e aspect ratio to pre7ent de%or'ationint side H "@in(!idth()6 height())int & H (!idth() - side / 2)Aint y H (height() - side / 2)A

    painter.set=ie!port(&6 y6 side6 side)A% !e 'ae the logical coordinate syste' a s"uare6 !e should also 'ae the 7ie!port as"uare using the Painterset=ie!port() %unction. n the e&a'ple abo7e !e 'ae ite"ui7alent to the largest s"uare that %it into the paint de7iceVs rectangle. ,y taing thepaint de7iceVs siIe into consideration !hen setting the !indo! or 7ie!port6 it is possible

    to eep the dra!ing code independent o% the paint de7ice.ote that the !indo!-7ie!port con7ersion is only a linear trans%or'ation6 i.e. it does notper%or' clipping. $his 'eans that i% you paint outside the currently set 4!indo!46 yourpainting is still trans%or'ed to the 7ie!port using the sa'e linear algebraic approach.

    $he 7ie!port6 4!indo!4 and trans%or'ation 'atri& deter'ine ho! logical Paintercoordinates 'ap to the paint de7iceVs physical coordinates. ,y de%ault the !orldtrans%or'ation 'atri& is the identity 'atri&6 and the 4!indo!4 and 7ie!port settings aree"ui7alent to the paint de7iceVs settings6 i.e. the !orld6 4!indo!4 and de7ice coordinatesyste's are e"ui7alent6 but as !e ha7e seen6 the syste's can be 'anipulated usingtrans%or'ation operations and !indo!-7ie!port con7ersion. $he illustration abo7edescribes the process.

    $he $rans%or'ations e&a'ple sho!s ho! trans%or'ations in%luence the !ay thatPainter renders graphics pri'iti7es. n particular it sho!s ho! the order o%trans%or'ations a%%ect the result.

    $he application allo!s the user to 'anipulate the rendering o% a shape by changing thetranslation6 rotation and scale o% PainterVs coordinate syste'.$he e&a'ple consists o% t!o classes and a global enu'$he 5ender

  • 7/27/2019 Qt Drawing Examples

    21/31

    cloc!ise using the Painterrotate() %unction and you can translate it (i.e. adding agi7en o%%set to the points) using the Paintertranslate() %unction. ou can also t!ist thecoordinate syste' around the origin (called shearing) using the Paintershear()%unction.

  • 7/27/2019 Qt Drawing Examples

    22/31

    operations6 a re%erence to its shape6 and a couple o% con7enience 7ariables that !e !illuse !hen rendering the coordinates.5ender

  • 7/27/2019 Qt Drawing Examples

    23/31

    return iIe(2326 232)AG;e rei'ple'ent the ;idgetVs 'ini'u'iIe8int() and siIe8int() %unctions to gi7e the5ender

  • 7/27/2019 Qt Drawing Examples

    24/31

    painter.dra!ine(06 06 R06 0)A painter.dra!ine(KN6 -26 R06 0)A painter.dra!ine(KN6 26 R06 0)A painter.dra!$e&t(Q0 - &,ounding5ect.!idth() / 26 0 S &,ounding5ect.height() / 26 tr(4&4))A

    painter.dra!ine(06 06 06 R0)A painter.dra!ine(-26 KN6 06 R0)A painter.dra!ine(26 KN6 06 R0)A painter.dra!$e&t(0 - y,ounding5ect.!idth() / 26 Q0 S y,ounding5ect.height() / 26 tr(4y4))AG

    7oid 5ender

  • 7/27/2019 Qt Drawing Examples

    25/31

    handler6 and trans%or's the gi7en PainterVs coordinate syste' according to the userVstrans%or'ation choices.;indo! Class De%inition$he ;indo! class is the $rans%or'ations applicationVs 'ain !indo!.$he application displays %our 5ender

  • 7/27/2019 Qt Drawing Examples

    26/31

    shapeCo'bo,o&-addte'(tr(4$e&t4))A shapeCo'bo,o&-addte'(tr(4$ruc4))A

    ridayout *layout H ne! ridayoutA layout-add;idget(original5ender

  • 7/27/2019 Qt Drawing Examples

    27/31

    ... shapes.append(cloc)A shapes.append(house)A shapes.append(te&t)A shapes.append(truc)A

    connect(shapeCo'bo,o&6

  • 7/27/2019 Qt Drawing Examples

    28/31

    coordinate syste'. $he order in !hich these tran%or'ations are applied is essential %orthe result.

  • 7/27/2019 Qt Drawing Examples

    29/31

    //// $hese are the other headers %or a7ailable 'ap tools (not used in this e&a'ple)////Binclude 4"gs'aptoolcapture.h4//Binclude 4"gs'aptoolidenti%y.h4//Binclude 4"gs'aptoolselect.h4

    //Binclude 4"gs'aptool7erte&edit.h4//Binclude 4"gs'easure.h4//

    @ain;indo!@ain;indo!(;idget* parent6 t;:lags %l) @ain;indo!(parent6%l)E //re"uired by tK to initialise the ui setup>i(this)A

    // nstantiate Pro7ider 5egistryBi% de%ined(+;+@

  • 7/27/2019 Qt Drawing Examples

    30/31

    //create a little toolbar 'p@ap$ool,ar H add$ool,ar(tr(4:ile4))A 'p@ap$ool,ar-add

  • 7/27/2019 Qt Drawing Examples

    31/31

    istgs@apCan7asayer 'yayeretA 'ypayer-set5enderer('yp5enderer)A

    i% ('ypayer-is=alid()) E "Debug(4ayer is 7alid4)A

    G else E "Debug(4ayer is 9$ 7alid4)A returnA G // 7h http//elgis.argeo.org/repos/R/elgis-release-R-R+0.noarch.rp'$he release 5P@ can be directly do!nloaded here http//elgis.argeo.org/repos/R/elgis-release-R-R+0.noarch.rp'

    http://elgis.argeo.org/repos/5/elgis-release-5-5_0.noarch.rpmhttp://elgis.argeo.org/repos/5/elgis-release-5-5_0.noarch.rpmhttp://elgis.argeo.org/repos/5/elgis-release-5-5_0.noarch.rpmhttp://elgis.argeo.org/repos/5/elgis-release-5-5_0.noarch.rpmhttp://elgis.argeo.org/repos/5/elgis-release-5-5_0.noarch.rpmhttp://elgis.argeo.org/repos/5/elgis-release-5-5_0.noarch.rpm