[190] | 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"><!--
|
---|
| 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>Picture</h1>
|
---|
| 33 |
|
---|
| 34 |
|
---|
| 35 | <p>
|
---|
| 36 | This example shows how to make a picture, store it to a file, and read it as
|
---|
| 37 | a 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 <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
| 51 | #include <<a href="qpainter-h.html">qpainter.h</a>>
|
---|
| 52 | #include <<a href="qpicture-h.html">qpicture.h</a>>
|
---|
| 53 | #include <<a href="qpixmap-h.html">qpixmap.h</a>>
|
---|
| 54 | #include <<a href="qwidget-h.html">qwidget.h</a>>
|
---|
| 55 | #include <<a href="qmessagebox-h.html">qmessagebox.h</a>>
|
---|
| 56 | #include <<a href="qfile-h.html">qfile.h</a>>
|
---|
| 57 | #include <ctype.h>
|
---|
| 58 |
|
---|
| 59 |
|
---|
| 60 | void 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-><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-><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-><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-><a href="qpainter.html#setBrush">setBrush</a>( brush ); // set brush
|
---|
| 75 | <a name="x116"></a> p-><a href="qpainter.html#drawRect">drawRect</a>( 80, 80, 250, 70 ); // car window
|
---|
| 76 | <a name="x117"></a> p-><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-><a href="qpainter.html#drawPixmap">drawPixmap</a>( 100, 85, pixmap );
|
---|
| 81 |
|
---|
| 82 | <a name="x119"></a> p-><a href="qpainter.html#setBackgroundMode">setBackgroundMode</a>( Qt::OpaqueMode ); // set opaque mode
|
---|
| 83 | p-><a href="qpainter.html#setBrush">setBrush</a>( Qt::DiagCrossPattern ); // black diagonal cross pattern
|
---|
| 84 | <a name="x112"></a> p-><a href="qpainter.html#drawEllipse">drawEllipse</a>( 90, 210, 80, 80 ); // back wheel
|
---|
| 85 | p-><a href="qpainter.html#setBrush">setBrush</a>( Qt::CrossPattern ); // black cross fill pattern
|
---|
| 86 | p-><a href="qpainter.html#drawEllipse">drawEllipse</a>( 310, 210, 80, 80 ); // front wheel
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 |
|
---|
| 90 | class PictureDisplay : public <a href="qwidget.html">QWidget</a> // picture display widget
|
---|
| 91 | {
|
---|
| 92 | public:
|
---|
| 93 | PictureDisplay( const char *fileName );
|
---|
| 94 | ~PictureDisplay();
|
---|
| 95 | protected:
|
---|
| 96 | void paintEvent( <a href="qpaintevent.html">QPaintEvent</a> * );
|
---|
| 97 | void keyPressEvent( <a href="qkeyevent.html">QKeyEvent</a> * );
|
---|
| 98 | private:
|
---|
| 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-><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 |
|
---|
| 114 | PictureDisplay::~PictureDisplay()
|
---|
| 115 | {
|
---|
| 116 | delete pict;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | void 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-><a href="qkeyevent.html#ascii">ascii</a>()) ) {
|
---|
| 131 | case 'r': // reload
|
---|
| 132 | pict-><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 |
|
---|
| 142 | int 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>( &pict ); // begin painting onto picture
|
---|
| 156 | paintCar( &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>( &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 © 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>
|
---|