📄 picture-example.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><!-- /home/reggie/tmp/qt-3.0-reggie-5401/qt-x11-commercial-3.0.5/examples/picture/picture.doc:4 --><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Picture</title><style type="text/css"><!--h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }a:link { color: #004faf; text-decoration: none }a:visited { color: #672967; text-decoration: none }body { background: #ffffff; color: black; }--></style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr bgcolor="#E5E5E5"><td valign=center> <a href="index.html"><font color="#004faf">Home</font></a> | <a href="classes.html"><font color="#004faf">All Classes</font></a> | <a href="mainclasses.html"><font color="#004faf">Main Classes</font></a> | <a href="annotated.html"><font color="#004faf">Annotated</font></a> | <a href="groups.html"><font color="#004faf">Grouped Classes</font></a> | <a href="functions.html"><font color="#004faf">Functions</font></a></td><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> <p> This example shows how to make a picture, store it to a file, and read it asa set of drawing commands.<p> <hr><p> Implementation:<p> <pre>/****************************************************************************** $Id: qt/picture.cpp 3.0.5 edited Oct 12 2001 $**** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.**** This file is part of an example program for Qt. This example** program may be used, distributed and modified without limitation.*******************************************************************************/#include <<a href="qapplication-h.html">qapplication.h</a>>#include <<a href="qpainter-h.html">qpainter.h</a>>#include <<a href="qpicture-h.html">qpicture.h</a>>#include <<a href="qpixmap-h.html">qpixmap.h</a>>#include <<a href="qwidget-h.html">qwidget.h</a>>#include <<a href="qmessagebox-h.html">qmessagebox.h</a>>#include <<a href="qfile-h.html">qfile.h</a>>#include <ctype.h>void paintCar( <a href="qpainter.html">QPainter</a> *p ) // paint a car{ <a href="qpointarray.html">QPointArray</a> a; <a href="qbrush.html">QBrush</a> brush( Qt::yellow, Qt::SolidPattern );<a name="x88"></a> p-><a href="qpainter.html#setBrush">setBrush</a>( brush ); // use solid, yellow brush<a name="x93"></a> a.setPoints( 5, 50,50, 350,50, 450,120, 450,250, 50,250 );<a name="x83"></a> p-><a href="qpainter.html#drawPolygon">drawPolygon</a>( a ); // draw car body <a href="qfont.html">QFont</a> f( "courier", 12, QFont::Bold );<a name="x89"></a> p-><a href="qpainter.html#setFont">setFont</a>( f ); <a href="qcolor.html">QColor</a> windowColor( 120, 120, 255 ); // a light blue color<a name="x76"></a> brush.<a href="qbrush.html#setColor">setColor</a>( windowColor ); // set this brush color p-><a href="qpainter.html#setBrush">setBrush</a>( brush ); // set brush<a name="x84"></a> p-><a href="qpainter.html#drawRect">drawRect</a>( 80, 80, 250, 70 ); // car window<a name="x85"></a> p-><a href="qpainter.html#drawText">drawText</a>( 180, 80, 150, 70, Qt::AlignCenter, "-- Qt --\nTrolltech AS" ); <a href="qpixmap.html">QPixmap</a> pixmap;<a name="x92"></a> if ( pixmap.<a href="qpixmap.html#load">load</a>("flag.bmp") ) // load and draw image<a name="x82"></a> p-><a href="qpainter.html#drawPixmap">drawPixmap</a>( 100, 85, pixmap );<a name="x87"></a> p-><a href="qpainter.html#setBackgroundMode">setBackgroundMode</a>( Qt::OpaqueMode ); // set opaque mode p-><a href="qpainter.html#setBrush">setBrush</a>( Qt::DiagCrossPattern ); // black diagonal cross pattern<a name="x80"></a> p-><a href="qpainter.html#drawEllipse">drawEllipse</a>( 90, 210, 80, 80 ); // back wheel p-><a href="qpainter.html#setBrush">setBrush</a>( Qt::CrossPattern ); // black cross fill pattern p-><a href="qpainter.html#drawEllipse">drawEllipse</a>( 310, 210, 80, 80 ); // front wheel}class PictureDisplay : public <a href="qwidget.html">QWidget</a> // picture display widget{public: PictureDisplay( const char *fileName ); ~PictureDisplay();protected: void paintEvent( <a href="qpaintevent.html">QPaintEvent</a> * ); void keyPressEvent( <a href="qkeyevent.html">QKeyEvent</a> * );private: <a href="qpicture.html">QPicture</a> *pict; <a href="qstring.html">QString</a> name;};<a name="f161"></a>PictureDisplay::PictureDisplay( const char *fileName ){ pict = new <a href="qpicture.html">QPicture</a>; name = fileName;<a name="x90"></a> if ( !pict-><a href="qpicture.html#load">load</a>(fileName) ) { // cannot load picture delete pict; pict = 0; name.<a href="qstring.html#sprintf">sprintf</a>( "Not able to load picture: %s", fileName ); }}PictureDisplay::~PictureDisplay(){ delete pict;}void PictureDisplay::<a href="qwidget.html#paintEvent">paintEvent</a>( <a href="qpaintevent.html">QPaintEvent</a> * ){ <a href="qpainter.html">QPainter</a> paint( this ); // paint widget if ( pict )<a name="x81"></a> paint.<a href="qpainter.html#drawPicture">drawPicture</a>( *pict ); // draw picture else paint.<a href="qpainter.html#drawText">drawText</a>( <a href="qwidget.html#rect">rect</a>(), AlignCenter, name );}<a name="x95"></a>void PictureDisplay::<a href="qwidget.html#keyPressEvent">keyPressEvent</a>( <a href="qkeyevent.html">QKeyEvent</a> *k ){<a name="x77"></a> switch ( tolower(k-><a href="qkeyevent.html#ascii">ascii</a>()) ) { case 'r': // reload pict-><a href="qpicture.html#load">load</a>( name ); <a href="qwidget.html#update">update</a>(); break; case 'q': // quit<a name="x74"></a> QApplication::<a href="qapplication.html#exit">exit</a>(); break; }}int main( int argc, char **argv ){ <a href="qapplication.html">QApplication</a> a( argc, argv ); // QApplication required! const char *fileName = "car.pic"; // default picture file name if ( argc == 2 ) // use argument as file name fileName = argv[1]; if ( !QFile::exists(fileName) ) { <a href="qpicture.html">QPicture</a> pict; // our picture <a href="qpainter.html">QPainter</a> paint; // our painter<a name="x79"></a> paint.<a href="qpainter.html#begin">begin</a>( &pict ); // begin painting onto picture paintCar( &paint ); // paint!<a name="x86"></a> paint.<a href="qpainter.html#end">end</a>(); // painting done<a name="x91"></a> pict.<a href="qpicture.html#save">save</a>( fileName ); // save picture<a name="x78"></a> QMessageBox::<a href="qmessagebox.html#information">information</a>(0, "Qt Example - Picture", "Saved. Run me again!"); return 0; } else { PictureDisplay test( fileName ); // create picture display a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &test); // set main widget test.<a href="qwidget.html#setCaption">setCaption</a>("Qt Example - Picture"); test.<a href="qwidget.html#show">show</a>(); // show it return a.<a href="qapplication.html#exec">exec</a>(); // start event loop }}</pre><p>See also <a href="examples.html">Examples</a>.<!-- eof --><p><address><hr><div align=center><table width=100% cellspacing=0 border=0><tr><td>Copyright © 2002 <a href="http://www.trolltech.com">Trolltech</a><td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a><td align=right><div align=right>Qt version 3.0.5</div></table></div></address></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -