1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
---|
2 | <!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/drawdemo/drawdemo.doc:4 -->
|
---|
3 | <html>
|
---|
4 | <head>
|
---|
5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
6 | <title>Draw Demo</title>
|
---|
7 | <style type="text/css"><!--
|
---|
8 | fn { margin-left: 1cm; text-indent: -1cm; }
|
---|
9 | a:link { color: #004faf; text-decoration: none }
|
---|
10 | a:visited { color: #672967; text-decoration: none }
|
---|
11 | body { background: #ffffff; color: black; }
|
---|
12 | --></style>
|
---|
13 | </head>
|
---|
14 | <body>
|
---|
15 |
|
---|
16 | <table border="0" cellpadding="0" cellspacing="0" width="100%">
|
---|
17 | <tr bgcolor="#E5E5E5">
|
---|
18 | <td valign=center>
|
---|
19 | <a href="index.html">
|
---|
20 | <font color="#004faf">Home</font></a>
|
---|
21 | | <a href="classes.html">
|
---|
22 | <font color="#004faf">All Classes</font></a>
|
---|
23 | | <a href="mainclasses.html">
|
---|
24 | <font color="#004faf">Main Classes</font></a>
|
---|
25 | | <a href="annotated.html">
|
---|
26 | <font color="#004faf">Annotated</font></a>
|
---|
27 | | <a href="groups.html">
|
---|
28 | <font color="#004faf">Grouped Classes</font></a>
|
---|
29 | | <a href="functions.html">
|
---|
30 | <font color="#004faf">Functions</font></a>
|
---|
31 | </td>
|
---|
32 | <td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Draw Demo</h1>
|
---|
33 |
|
---|
34 |
|
---|
35 | <p>
|
---|
36 | This example demonstrates several drawing functions and printer output.
|
---|
37 | You can easily add you own drawing functions.
|
---|
38 | <p> <hr>
|
---|
39 | <p> Implementation:
|
---|
40 | <p> <pre>/****************************************************************************
|
---|
41 | ** $Id: drawdemo-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
42 | **
|
---|
43 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
44 | **
|
---|
45 | ** This file is part of an example program for Qt. This example
|
---|
46 | ** program may be used, distributed and modified without limitation.
|
---|
47 | **
|
---|
48 | *****************************************************************************/
|
---|
49 |
|
---|
50 | #include <<a href="qwidget-h.html">qwidget.h</a>>
|
---|
51 | #include <<a href="qpainter-h.html">qpainter.h</a>>
|
---|
52 | #include <<a href="qprinter-h.html">qprinter.h</a>>
|
---|
53 | #include <<a href="qpushbutton-h.html">qpushbutton.h</a>>
|
---|
54 | #include <<a href="qradiobutton-h.html">qradiobutton.h</a>>
|
---|
55 | #include <<a href="qbuttongroup-h.html">qbuttongroup.h</a>>
|
---|
56 | #include <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
57 | #include <math.h>
|
---|
58 |
|
---|
59 | //
|
---|
60 | // First we define the functionality our demo should present
|
---|
61 | // to the user. You might add different demo-modes if you wish so.
|
---|
62 | //
|
---|
63 |
|
---|
64 | //
|
---|
65 | // This function draws a color wheel.
|
---|
66 | // The coordinate system x=(0..500), y=(0..500) spans the paint device.
|
---|
67 | //
|
---|
68 |
|
---|
69 | void drawColorWheel( <a href="qpainter.html">QPainter</a> *p )
|
---|
70 | {
|
---|
71 | <a href="qfont.html">QFont</a> f( "times", 18, QFont::Bold );
|
---|
72 | <a name="x1075"></a> p-><a href="qpainter.html#setFont">setFont</a>( f );
|
---|
73 | <a name="x1076"></a> p-><a href="qpainter.html#setPen">setPen</a>( Qt::black );
|
---|
74 | <a name="x1077"></a> p-><a href="qpainter.html#setWindow">setWindow</a>( 0, 0, 500, 500 ); // defines coordinate system
|
---|
75 |
|
---|
76 | for ( int i=0; i<36; i++ ) { // draws 36 rotated rectangles
|
---|
77 |
|
---|
78 | <a href="qwmatrix.html">QWMatrix</a> matrix;
|
---|
79 | <a name="x1097"></a> matrix.<a href="qwmatrix.html#translate">translate</a>( 250.0F, 250.0F ); // move to center
|
---|
80 | <a name="x1096"></a> matrix.<a href="qwmatrix.html#shear">shear</a>( 0.0F, 0.3F ); // twist it
|
---|
81 | <a name="x1095"></a> matrix.<a href="qwmatrix.html#rotate">rotate</a>( (float)i*10 ); // rotate 0,10,20,.. degrees
|
---|
82 | <a name="x1078"></a> p-><a href="qpainter.html#setWorldMatrix">setWorldMatrix</a>( matrix ); // use this world matrix
|
---|
83 |
|
---|
84 | <a href="qcolor.html">QColor</a> c;
|
---|
85 | <a name="x1063"></a> c.<a href="qcolor.html#setHsv">setHsv</a>( i*10, 255, 255 ); // rainbow effect
|
---|
86 | <a name="x1074"></a> p-><a href="qpainter.html#setBrush">setBrush</a>( c ); // solid fill with color c
|
---|
87 | <a name="x1070"></a> p-><a href="qpainter.html#drawRect">drawRect</a>( 70, -10, 80, 10 ); // draw the rectangle
|
---|
88 |
|
---|
89 | <a href="qstring.html">QString</a> n;
|
---|
90 | n.<a href="qstring.html#sprintf">sprintf</a>( "H=%d", i*10 );
|
---|
91 | <a name="x1072"></a> p-><a href="qpainter.html#drawText">drawText</a>( 80+70+5, 0, n ); // draw the hue number
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | //
|
---|
97 | // This function draws a few lines of text using different fonts.
|
---|
98 | //
|
---|
99 |
|
---|
100 | void drawFonts( <a href="qpainter.html">QPainter</a> *p )
|
---|
101 | {
|
---|
102 | static const char *fonts[] = { "Helvetica", "Courier", "Times", 0 };
|
---|
103 | static int sizes[] = { 10, 12, 18, 24, 36, 0 };
|
---|
104 | int f = 0;
|
---|
105 | int y = 0;
|
---|
106 | while ( fonts[f] ) {
|
---|
107 | int s = 0;
|
---|
108 | while ( sizes[s] ) {
|
---|
109 | <a href="qfont.html">QFont</a> font( fonts[f], sizes[s] );
|
---|
110 | p-><a href="qpainter.html#setFont">setFont</a>( font );
|
---|
111 | <a name="x1073"></a> <a href="qfontmetrics.html">QFontMetrics</a> fm = p-><a href="qpainter.html#fontMetrics">fontMetrics</a>();
|
---|
112 | <a name="x1064"></a> y += fm.<a href="qfontmetrics.html#ascent">ascent</a>();
|
---|
113 | p-><a href="qpainter.html#drawText">drawText</a>( 10, y, "Quartz Glyph Job Vex'd Cwm Finks" );
|
---|
114 | <a name="x1065"></a> y += fm.<a href="qfontmetrics.html#descent">descent</a>();
|
---|
115 | s++;
|
---|
116 | }
|
---|
117 | f++;
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | //
|
---|
123 | // This function draws some shapes
|
---|
124 | //
|
---|
125 |
|
---|
126 | void drawShapes( <a href="qpainter.html">QPainter</a> *p )
|
---|
127 | {
|
---|
128 | <a href="qbrush.html">QBrush</a> b1( Qt::blue );
|
---|
129 | <a href="qbrush.html">QBrush</a> b2( Qt::green, Qt::Dense6Pattern ); // green 12% fill
|
---|
130 | <a href="qbrush.html">QBrush</a> b3( Qt::NoBrush ); // void brush
|
---|
131 | <a href="qbrush.html">QBrush</a> b4( Qt::CrossPattern ); // black cross pattern
|
---|
132 |
|
---|
133 | p-><a href="qpainter.html#setPen">setPen</a>( Qt::red );
|
---|
134 | p-><a href="qpainter.html#setBrush">setBrush</a>( b1 );
|
---|
135 | p-><a href="qpainter.html#drawRect">drawRect</a>( 10, 10, 200, 100 );
|
---|
136 | p-><a href="qpainter.html#setBrush">setBrush</a>( b2 );
|
---|
137 | <a name="x1071"></a> p-><a href="qpainter.html#drawRoundRect">drawRoundRect</a>( 10, 150, 200, 100, 20, 20 );
|
---|
138 | p-><a href="qpainter.html#setBrush">setBrush</a>( b3 );
|
---|
139 | <a name="x1068"></a> p-><a href="qpainter.html#drawEllipse">drawEllipse</a>( 250, 10, 200, 100 );
|
---|
140 | p-><a href="qpainter.html#setBrush">setBrush</a>( b4 );
|
---|
141 | <a name="x1069"></a> p-><a href="qpainter.html#drawPie">drawPie</a>( 250, 150, 200, 100, 45*16, 90*16 );
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | typedef void (*draw_func)(QPainter*);
|
---|
146 |
|
---|
147 | struct DrawThing {
|
---|
148 | draw_func f;
|
---|
149 | const char *name;
|
---|
150 | };
|
---|
151 |
|
---|
152 | //
|
---|
153 | // All previously implemented functions are collected
|
---|
154 | // in the following "table".
|
---|
155 | // If you implement different functionality, your new draw
|
---|
156 | // function must be assigned here with a function pointer and
|
---|
157 | // description.
|
---|
158 | // Leave the zeros at the end, they will be used
|
---|
159 | // as markers referring to the end of the array.
|
---|
160 | //
|
---|
161 |
|
---|
162 | DrawThing ourDrawFunctions[] = {
|
---|
163 | // name of the function, title presented to the user
|
---|
164 | { drawColorWheel, "Draw color wheel" },
|
---|
165 | { drawFonts, "Draw fonts" },
|
---|
166 | { drawShapes, "Draw shapes" },
|
---|
167 | { 0, 0 } };
|
---|
168 |
|
---|
169 |
|
---|
170 |
|
---|
171 | class DrawView : public <a href="qwidget.html">QWidget</a>
|
---|
172 | {
|
---|
173 | <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
|
---|
174 | public:
|
---|
175 | DrawView();
|
---|
176 | ~DrawView();
|
---|
177 | public slots:
|
---|
178 | void updateIt( int );
|
---|
179 | void printIt();
|
---|
180 | protected:
|
---|
181 | void drawIt( <a href="qpainter.html">QPainter</a> * );
|
---|
182 | void paintEvent( <a href="qpaintevent.html">QPaintEvent</a> * );
|
---|
183 | void resizeEvent( <a href="qresizeevent.html">QResizeEvent</a> * );
|
---|
184 | private:
|
---|
185 | <a href="qprinter.html">QPrinter</a> *printer;
|
---|
186 | <a href="qbuttongroup.html">QButtonGroup</a> *bgroup;
|
---|
187 | <a href="qpushbutton.html">QPushButton</a> *print;
|
---|
188 | int drawindex;
|
---|
189 | int maxindex;
|
---|
190 | };
|
---|
191 |
|
---|
192 |
|
---|
193 | //
|
---|
194 | // Construct the DrawView with buttons.
|
---|
195 | //
|
---|
196 |
|
---|
197 | <a name="f367"></a>DrawView::DrawView()
|
---|
198 | {
|
---|
199 | <a href="qwidget.html#setCaption">setCaption</a>( "Qt Draw Demo Application" );
|
---|
200 | <a href="qwidget.html#setBackgroundMode">setBackgroundMode</a>(PaletteBase);
|
---|
201 |
|
---|
202 | // Create a button group to contain all buttons
|
---|
203 | bgroup = new <a href="qbuttongroup.html">QButtonGroup</a>( this );
|
---|
204 | <a name="x1088"></a> bgroup-><a href="qwidget.html#resize">resize</a>( 200, 200 );
|
---|
205 | <a name="x1062"></a> <a href="qobject.html#connect">connect</a>( bgroup, SIGNAL(<a href="qbuttongroup.html#clicked">clicked</a>(int)), SLOT(updateIt(int)) );
|
---|
206 |
|
---|
207 | // Calculate the size for the radio buttons
|
---|
208 | int maxwidth = 80;
|
---|
209 | int maxheight = 10;
|
---|
210 | int i;
|
---|
211 | const char *n;
|
---|
212 | <a name="x1084"></a> <a href="qfontmetrics.html">QFontMetrics</a> fm = bgroup-><a href="qwidget.html#fontMetrics">fontMetrics</a>();
|
---|
213 |
|
---|
214 | // Find out the longest function description.
|
---|
215 | // Here we make use of the last "0,0"-entry in the
|
---|
216 | // ourDrawFunctions-array.
|
---|
217 | for ( i=0; (n=ourDrawFunctions[i].name) != 0; i++ ) {
|
---|
218 | <a name="x1066"></a> int w = fm.<a href="qfontmetrics.html#width">width</a>( n );
|
---|
219 | maxwidth = QMAX(w,maxwidth); // QMAX is a macro defined in qglobal.h
|
---|
220 | // and returns the biggest of to values.
|
---|
221 | // Due to its macro nature one should use it with care and with
|
---|
222 | // constant parameters only.
|
---|
223 | }
|
---|
224 |
|
---|
225 | maxwidth = maxwidth + 30; // allow 30 pixels for radiobuttons
|
---|
226 |
|
---|
227 | for ( i=0; (n=ourDrawFunctions[i].name) != 0; i++ ) {
|
---|
228 | <a href="qradiobutton.html">QRadioButton</a> *rb = new <a href="qradiobutton.html">QRadioButton</a>( n, bgroup );
|
---|
229 | <a name="x1091"></a> rb-><a href="qwidget.html#setGeometry">setGeometry</a>( 10, i*30+10, maxwidth, 30 );
|
---|
230 |
|
---|
231 | maxheight += 30;
|
---|
232 |
|
---|
233 | if ( i == 0 )
|
---|
234 | <a name="x1082"></a> rb-><a href="qradiobutton.html#setChecked">setChecked</a>( TRUE );
|
---|
235 | }
|
---|
236 |
|
---|
237 | maxheight += 10; // maxheight is now 10 pixels upper margin
|
---|
238 | // plus number_of_drawfunctions * 30
|
---|
239 | // plus 10 pixels lower margin
|
---|
240 |
|
---|
241 | drawindex = 0; // draw first thing
|
---|
242 | maxindex = i;
|
---|
243 |
|
---|
244 | maxwidth += 20; // add some margin, this results in the
|
---|
245 | // final width of bgroup
|
---|
246 |
|
---|
247 | bgroup-><a href="qwidget.html#resize">resize</a>( maxwidth, maxheight ); // resize bgroup to its final size
|
---|
248 | // when no printersupport is provided
|
---|
249 |
|
---|
250 |
|
---|
251 | // If -- at compile time -- printer support will be disabled,
|
---|
252 | // we won't set up printing functionality.
|
---|
253 |
|
---|
254 | #ifndef QT_NO_PRINTER
|
---|
255 |
|
---|
256 | printer = new <a href="qprinter.html">QPrinter</a>;
|
---|
257 |
|
---|
258 | // Create and setup the print button
|
---|
259 | print = new <a href="qpushbutton.html">QPushButton</a>( "Print...", bgroup );
|
---|
260 | <a name="x1081"></a> print-><a href="qwidget.html#resize">resize</a>( 80, 30 );
|
---|
261 | <a name="x1093"></a><a name="x1080"></a> print-><a href="qwidget.html#move">move</a>( maxwidth/2 - print-><a href="qwidget.html#width">width</a>()/2, maxindex*30+20 );
|
---|
262 | <a href="qobject.html#connect">connect</a>( print, SIGNAL(<a href="qbutton.html#clicked">clicked</a>()), SLOT(printIt()) );
|
---|
263 |
|
---|
264 | // Resize bgroup to its final size when printersupport is given.
|
---|
265 | <a name="x1094"></a><a name="x1085"></a> bgroup-><a href="qwidget.html#resize">resize</a>( maxwidth, print-><a href="qwidget.html#y">y</a>()+print-><a href="qwidget.html#height">height</a>()+10 );
|
---|
266 |
|
---|
267 | #endif
|
---|
268 |
|
---|
269 | <a href="qwidget.html#resize">resize</a>( 640,300 );
|
---|
270 | }
|
---|
271 |
|
---|
272 | //
|
---|
273 | // Clean up.
|
---|
274 | //
|
---|
275 | DrawView::~DrawView()
|
---|
276 | {
|
---|
277 | #ifndef QT_NO_PRINTER
|
---|
278 | delete printer;
|
---|
279 | #endif
|
---|
280 | }
|
---|
281 |
|
---|
282 | //
|
---|
283 | // Called when a radio button is clicked.
|
---|
284 | //
|
---|
285 |
|
---|
286 | void <a name="f368"></a>DrawView::updateIt( int index )
|
---|
287 | {
|
---|
288 | if ( index < maxindex ) {
|
---|
289 | drawindex = index;
|
---|
290 | <a href="qwidget.html#update">update</a>();
|
---|
291 | }
|
---|
292 | }
|
---|
293 |
|
---|
294 | //
|
---|
295 | // Calls the drawing function as specified by the radio buttons.
|
---|
296 | //
|
---|
297 |
|
---|
298 | void <a name="f369"></a>DrawView::drawIt( <a href="qpainter.html">QPainter</a> *p )
|
---|
299 | {
|
---|
300 | (*ourDrawFunctions[drawindex].f)(p);
|
---|
301 | }
|
---|
302 |
|
---|
303 | //
|
---|
304 | // Called when the print button is clicked.
|
---|
305 | //
|
---|
306 |
|
---|
307 | void <a name="f370"></a>DrawView::printIt()
|
---|
308 | {
|
---|
309 | <a name="x1079"></a> if ( printer-><a href="qprinter.html#setup">setup</a>( this ) ) {
|
---|
310 | <a href="qpainter.html">QPainter</a> paint;
|
---|
311 | <a name="x1067"></a> if( !paint.<a href="qpainter.html#begin">begin</a>( printer ) )
|
---|
312 | return;
|
---|
313 | drawIt( &paint );
|
---|
314 | }
|
---|
315 | }
|
---|
316 |
|
---|
317 | //
|
---|
318 | // Called when the widget needs to be updated.
|
---|
319 | //
|
---|
320 |
|
---|
321 | void DrawView::<a href="qwidget.html#paintEvent">paintEvent</a>( <a href="qpaintevent.html">QPaintEvent</a> * )
|
---|
322 | {
|
---|
323 | <a href="qpainter.html">QPainter</a> paint( this );
|
---|
324 | drawIt( &paint );
|
---|
325 | }
|
---|
326 |
|
---|
327 | //
|
---|
328 | // Called when the widget has been resized.
|
---|
329 | // Moves the button group to the upper right corner
|
---|
330 | // of the widget.
|
---|
331 |
|
---|
332 | <a name="x1089"></a>void DrawView::<a href="qwidget.html#resizeEvent">resizeEvent</a>( <a href="qresizeevent.html">QResizeEvent</a> * )
|
---|
333 | {
|
---|
334 | <a name="x1086"></a> bgroup-><a href="qwidget.html#move">move</a>( <a href="qwidget.html#width">width</a>()-bgroup-><a href="qwidget.html#width">width</a>(), 0 );
|
---|
335 | }
|
---|
336 |
|
---|
337 |
|
---|
338 | //
|
---|
339 | // Create and display our widget.
|
---|
340 | //
|
---|
341 |
|
---|
342 | #include "drawdemo.moc"
|
---|
343 |
|
---|
344 | int main( int argc, char **argv )
|
---|
345 | {
|
---|
346 | <a href="qapplication.html">QApplication</a> app( argc, argv );
|
---|
347 | DrawView draw;
|
---|
348 | app.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &draw );
|
---|
349 | draw.<a href="qwidget.html#setCaption">setCaption</a>("Qt Example - Drawdemo");
|
---|
350 | draw.<a href="qwidget.html#show">show</a>();
|
---|
351 | return app.<a href="qapplication.html#exec">exec</a>();
|
---|
352 | }
|
---|
353 | </pre>
|
---|
354 |
|
---|
355 | <p>See also <a href="examples.html">Examples</a>.
|
---|
356 |
|
---|
357 | <!-- eof -->
|
---|
358 | <p><address><hr><div align=center>
|
---|
359 | <table width=100% cellspacing=0 border=0><tr>
|
---|
360 | <td>Copyright © 2007
|
---|
361 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
362 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
363 | </table></div></address></body>
|
---|
364 | </html>
|
---|