source: trunk/doc/html/picture-example.html

Last change on this file was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 8.4 KB
Line 
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/picture/picture.doc:4 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>Picture</title>
7<style type="text/css"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { 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&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;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&nbsp;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>Picture</h1>
33
34
35<p>
36This example shows how to make a picture, store it to a file, and read it as
37a set of drawing commands.
38<p> <hr>
39<p> Implementation:
40<p> <pre>/****************************************************************************
41** $Id: picture-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 &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
51#include &lt;<a href="qpainter-h.html">qpainter.h</a>&gt;
52#include &lt;<a href="qpicture-h.html">qpicture.h</a>&gt;
53#include &lt;<a href="qpixmap-h.html">qpixmap.h</a>&gt;
54#include &lt;<a href="qwidget-h.html">qwidget.h</a>&gt;
55#include &lt;<a href="qmessagebox-h.html">qmessagebox.h</a>&gt;
56#include &lt;<a href="qfile-h.html">qfile.h</a>&gt;
57#include &lt;ctype.h&gt;
58
59
60void paintCar( <a href="qpainter.html">QPainter</a> *p ) // paint a car
61{
62 <a href="qpointarray.html">QPointArray</a> a;
63 <a href="qbrush.html">QBrush</a> brush( Qt::yellow, Qt::SolidPattern );
64<a name="x120"></a> p-&gt;<a href="qpainter.html#setBrush">setBrush</a>( brush ); // use solid, yellow brush
65
66<a name="x125"></a> a.setPoints( 5, 50,50, 350,50, 450,120, 450,250, 50,250 );
67<a name="x115"></a> p-&gt;<a href="qpainter.html#drawPolygon">drawPolygon</a>( a ); // draw car body
68
69 <a href="qfont.html">QFont</a> f( "courier", 12, QFont::Bold );
70<a name="x121"></a> p-&gt;<a href="qpainter.html#setFont">setFont</a>( f );
71
72 <a href="qcolor.html">QColor</a> windowColor( 120, 120, 255 ); // a light blue color
73<a name="x108"></a> brush.<a href="qbrush.html#setColor">setColor</a>( windowColor ); // set this brush color
74 p-&gt;<a href="qpainter.html#setBrush">setBrush</a>( brush ); // set brush
75<a name="x116"></a> p-&gt;<a href="qpainter.html#drawRect">drawRect</a>( 80, 80, 250, 70 ); // car window
76<a name="x117"></a> p-&gt;<a href="qpainter.html#drawText">drawText</a>( 180, 80, 150, 70, Qt::AlignCenter, "-- Qt --\nTrolltech AS" );
77
78 <a href="qpixmap.html">QPixmap</a> pixmap;
79<a name="x124"></a> if ( pixmap.<a href="qpixmap.html#load">load</a>("flag.bmp") ) // load and draw image
80<a name="x114"></a> p-&gt;<a href="qpainter.html#drawPixmap">drawPixmap</a>( 100, 85, pixmap );
81
82<a name="x119"></a> p-&gt;<a href="qpainter.html#setBackgroundMode">setBackgroundMode</a>( Qt::OpaqueMode ); // set opaque mode
83 p-&gt;<a href="qpainter.html#setBrush">setBrush</a>( Qt::DiagCrossPattern ); // black diagonal cross pattern
84<a name="x112"></a> p-&gt;<a href="qpainter.html#drawEllipse">drawEllipse</a>( 90, 210, 80, 80 ); // back wheel
85 p-&gt;<a href="qpainter.html#setBrush">setBrush</a>( Qt::CrossPattern ); // black cross fill pattern
86 p-&gt;<a href="qpainter.html#drawEllipse">drawEllipse</a>( 310, 210, 80, 80 ); // front wheel
87}
88
89
90class PictureDisplay : public <a href="qwidget.html">QWidget</a> // picture display widget
91{
92public:
93 PictureDisplay( const char *fileName );
94 ~PictureDisplay();
95protected:
96 void paintEvent( <a href="qpaintevent.html">QPaintEvent</a> * );
97 void keyPressEvent( <a href="qkeyevent.html">QKeyEvent</a> * );
98private:
99 <a href="qpicture.html">QPicture</a> *pict;
100 <a href="qstring.html">QString</a> name;
101};
102
103<a name="f207"></a>PictureDisplay::PictureDisplay( const char *fileName )
104{
105 pict = new <a href="qpicture.html">QPicture</a>;
106 name = fileName;
107<a name="x122"></a> if ( !pict-&gt;<a href="qpicture.html#load">load</a>(fileName) ) { // cannot load picture
108 delete pict;
109 pict = 0;
110 name.<a href="qstring.html#sprintf">sprintf</a>( "Not able to load picture: %s", fileName );
111 }
112}
113
114PictureDisplay::~PictureDisplay()
115{
116 delete pict;
117}
118
119void PictureDisplay::<a href="qwidget.html#paintEvent">paintEvent</a>( <a href="qpaintevent.html">QPaintEvent</a> * )
120{
121 <a href="qpainter.html">QPainter</a> paint( this ); // paint widget
122 if ( pict )
123<a name="x113"></a> paint.<a href="qpainter.html#drawPicture">drawPicture</a>( *pict ); // draw picture
124 else
125 paint.<a href="qpainter.html#drawText">drawText</a>( <a href="qwidget.html#rect">rect</a>(), AlignCenter, name );
126}
127
128<a name="x127"></a>void PictureDisplay::<a href="qwidget.html#keyPressEvent">keyPressEvent</a>( <a href="qkeyevent.html">QKeyEvent</a> *k )
129{
130<a name="x109"></a> switch ( tolower(k-&gt;<a href="qkeyevent.html#ascii">ascii</a>()) ) {
131 case 'r': // reload
132 pict-&gt;<a href="qpicture.html#load">load</a>( name );
133 <a href="qwidget.html#update">update</a>();
134 break;
135 case 'q': // quit
136<a name="x106"></a> QApplication::<a href="qapplication.html#exit">exit</a>();
137 break;
138 }
139}
140
141
142int main( int argc, char **argv )
143{
144 <a href="qapplication.html">QApplication</a> a( argc, argv ); // QApplication required!
145
146 const char *fileName = "car.pic"; // default picture file name
147
148 if ( argc == 2 ) // use argument as file name
149 fileName = argv[1];
150
151 if ( !QFile::exists(fileName) ) {
152 <a href="qpicture.html">QPicture</a> pict; // our picture
153 <a href="qpainter.html">QPainter</a> paint; // our painter
154
155<a name="x111"></a> paint.<a href="qpainter.html#begin">begin</a>( &amp;pict ); // begin painting onto picture
156 paintCar( &amp;paint ); // paint!
157<a name="x118"></a> paint.<a href="qpainter.html#end">end</a>(); // painting done
158
159<a name="x123"></a> pict.<a href="qpicture.html#save">save</a>( fileName ); // save picture
160<a name="x110"></a> QMessageBox::<a href="qmessagebox.html#information">information</a>(0, "Qt Example - Picture", "Saved. Run me again!");
161 return 0;
162 } else {
163 PictureDisplay test( fileName ); // create picture display
164 a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &amp;test); // set main widget
165 test.<a href="qwidget.html#setCaption">setCaption</a>("Qt Example - Picture");
166 test.<a href="qwidget.html#show">show</a>(); // show it
167
168 return a.<a href="qapplication.html#exec">exec</a>(); // start event loop
169 }
170}
171</pre>
172
173<p>See also <a href="examples.html">Examples</a>.
174
175<!-- eof -->
176<p><address><hr><div align=center>
177<table width=100% cellspacing=0 border=0><tr>
178<td>Copyright &copy; 2007
179<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
180<td align=right><div align=right>Qt 3.3.8</div>
181</table></div></address></body>
182</html>
Note: See TracBrowser for help on using the repository browser.