drawdemo-example.html

来自「QT 下载资料仅供参考」· HTML 代码 · 共 363 行

HTML
363
字号
<!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/drawdemo/drawdemo.doc:4 --><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Draw Demo</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&nbsp;Classes</font></a> | <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a> | <a href="annotated.html"><font color="#004faf">Annotated</font></a> | <a href="groups.html"><font color="#004faf">Grouped&nbsp;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>Draw Demo</h1>   <p> This example demonstrates several drawing functions and printer output.You can easily add you own drawing functions.<p> <hr><p> Implementation:<p> <pre>/****************************************************************************** $Id:  qt/drawdemo.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 &lt;<a href="qwidget-h.html">qwidget.h</a>&gt;#include &lt;<a href="qpainter-h.html">qpainter.h</a>&gt;#include &lt;<a href="qprinter-h.html">qprinter.h</a>&gt;#include &lt;<a href="qpushbutton-h.html">qpushbutton.h</a>&gt;#include &lt;<a href="qradiobutton-h.html">qradiobutton.h</a>&gt;#include &lt;<a href="qbuttongroup-h.html">qbuttongroup.h</a>&gt;#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;#include &lt;math.h&gt;//// First we define the functionality our demo should present// to the user. You might add different demo-modes if you wish so.////// This function draws a color wheel.// The coordinate system x=(0..500), y=(0..500) spans the paint device.//void drawColorWheel( <a href="qpainter.html">QPainter</a> *p ){    <a href="qfont.html">QFont</a> f( "times", 18, QFont::Bold );<a name="x1176"></a>    p-&gt;<a href="qpainter.html#setFont">setFont</a>( f );<a name="x1177"></a>    p-&gt;<a href="qpainter.html#setPen">setPen</a>( Qt::black );<a name="x1178"></a>    p-&gt;<a href="qpainter.html#setWindow">setWindow</a>( 0, 0, 500, 500 );             // defines coordinate system    for ( int i=0; i&lt;36; i++ ) {                // draws 36 rotated rectangles        <a href="qwmatrix.html">QWMatrix</a> matrix;<a name="x1198"></a>        matrix.<a href="qwmatrix.html#translate">translate</a>( 250.0F, 250.0F );     // move to center<a name="x1197"></a>        matrix.<a href="qwmatrix.html#shear">shear</a>( 0.0F, 0.3F );             // twist it<a name="x1196"></a>        matrix.<a href="qwmatrix.html#rotate">rotate</a>( (float)i*10 );           // rotate 0,10,20,.. degrees<a name="x1179"></a>        p-&gt;<a href="qpainter.html#setWorldMatrix">setWorldMatrix</a>( matrix );            // use this world matrix        <a href="qcolor.html">QColor</a> c;<a name="x1165"></a>        c.<a href="qcolor.html#setHsv">setHsv</a>( i*10, 255, 255 );             // rainbow effect<a name="x1175"></a>        p-&gt;<a href="qpainter.html#setBrush">setBrush</a>( c );                       // solid fill with color c<a name="x1171"></a>        p-&gt;<a href="qpainter.html#drawRect">drawRect</a>( 70, -10, 80, 10 );         // draw the rectangle        <a href="qstring.html">QString</a> n;        n.<a href="qstring.html#sprintf">sprintf</a>( "H=%d", i*10 );<a name="x1173"></a>        p-&gt;<a href="qpainter.html#drawText">drawText</a>( 80+70+5, 0, n );           // draw the hue number    }}//// This function draws a few lines of text using different fonts.//void drawFonts( <a href="qpainter.html">QPainter</a> *p ){    static const char *fonts[] = { "Helvetica", "Courier", "Times", 0 };    static int   sizes[] = { 10, 12, 18, 24, 36, 0 };    int f = 0;    int y = 0;    while ( fonts[f] ) {        int s = 0;        while ( sizes[s] ) {            <a href="qfont.html">QFont</a> font( fonts[f], sizes[s] );            p-&gt;<a href="qpainter.html#setFont">setFont</a>( font );<a name="x1174"></a>            <a href="qfontmetrics.html">QFontMetrics</a> fm = p-&gt;<a href="qpainter.html#fontMetrics">fontMetrics</a>();<a name="x1166"></a>            y += fm.<a href="qfontmetrics.html#ascent">ascent</a>();            p-&gt;<a href="qpainter.html#drawText">drawText</a>( 10, y, "Quartz Glyph Job Vex'd Cwm Finks" );<a name="x1167"></a>            y += fm.<a href="qfontmetrics.html#descent">descent</a>();            s++;        }        f++;    }}//// This function draws some shapes//void drawShapes( <a href="qpainter.html">QPainter</a> *p ){    <a href="qbrush.html">QBrush</a> b1( Qt::blue );    <a href="qbrush.html">QBrush</a> b2( Qt::green, Qt::Dense6Pattern );          // green 12% fill    <a href="qbrush.html">QBrush</a> b3( Qt::NoBrush );                           // void brush    <a href="qbrush.html">QBrush</a> b4( Qt::CrossPattern );                      // black cross pattern    p-&gt;<a href="qpainter.html#setPen">setPen</a>( Qt::red );    p-&gt;<a href="qpainter.html#setBrush">setBrush</a>( b1 );    p-&gt;<a href="qpainter.html#drawRect">drawRect</a>( 10, 10, 200, 100 );    p-&gt;<a href="qpainter.html#setBrush">setBrush</a>( b2 );<a name="x1172"></a>    p-&gt;<a href="qpainter.html#drawRoundRect">drawRoundRect</a>( 10, 150, 200, 100, 20, 20 );    p-&gt;<a href="qpainter.html#setBrush">setBrush</a>( b3 );<a name="x1169"></a>    p-&gt;<a href="qpainter.html#drawEllipse">drawEllipse</a>( 250, 10, 200, 100 );    p-&gt;<a href="qpainter.html#setBrush">setBrush</a>( b4 );<a name="x1170"></a>    p-&gt;<a href="qpainter.html#drawPie">drawPie</a>( 250, 150, 200, 100, 45*16, 90*16 );}typedef void (*draw_func)(QPainter*);struct DrawThing {    draw_func    f;    const char  *name;};//// All previously implemented functions are collected// in the following "table".// If you implement different functionality, your new draw// function must be assigned here with a function pointer and// description.// Leave the zeros at the end, they will be used// as markers referring to the end of the array.//DrawThing ourDrawFunctions[] = {// name of the function, title presented to the user    { drawColorWheel,   "Draw color wheel" },    { drawFonts,        "Draw fonts" },    { drawShapes,       "Draw shapes" },    { 0,                0 } };class DrawView : public <a href="qwidget.html">QWidget</a>{    <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>public:    DrawView();    ~DrawView();public slots:    void   updateIt( int );    void   printIt();protected:    void   drawIt( <a href="qpainter.html">QPainter</a> * );    void   paintEvent( <a href="qpaintevent.html">QPaintEvent</a> * );    void   resizeEvent( <a href="qresizeevent.html">QResizeEvent</a> * );private:    <a href="qprinter.html">QPrinter</a>     *printer;    <a href="qbuttongroup.html">QButtonGroup</a> *bgroup;    <a href="qpushbutton.html">QPushButton</a>  *print;    int           drawindex;    int           maxindex;};//// Construct the DrawView with buttons.//<a name="f377"></a>DrawView::DrawView(){    <a href="qwidget.html#setCaption">setCaption</a>( "Qt Draw Demo Application" );    <a href="qwidget.html#setBackgroundColor">setBackgroundColor</a>( white );    // Create a button group to contain all buttons    bgroup = new <a href="qbuttongroup.html">QButtonGroup</a>( this );<a name="x1189"></a>    bgroup-&gt;<a href="qwidget.html#resize">resize</a>( 200, 200 );<a name="x1164"></a>    <a href="qobject.html#connect">connect</a>( bgroup, SIGNAL(<a href="qbuttongroup.html#clicked">clicked</a>(int)), SLOT(updateIt(int)) );    // Calculate the size for the radio buttons    int maxwidth = 80;    int maxheight = 10;    int i;    const char *n;<a name="x1185"></a>    <a href="qfontmetrics.html">QFontMetrics</a> fm = bgroup-&gt;<a href="qwidget.html#fontMetrics">fontMetrics</a>();    // Find out the longest function description.    // Here we make use of the last "0,0"-entry in the    // ourDrawFunctions-array.    for ( i=0; (n=ourDrawFunctions[i].name) != 0; i++ ) {<a name="x1168"></a>        int w = fm.<a href="qfontmetrics.html#width">width</a>( n );        maxwidth = QMAX(w,maxwidth); // QMAX is a macro defined in qglobal.h                                     // and returns the biggest of to values.        // Due to its macro nature one should use it with care and with        // constant parameters only.    }    maxwidth = maxwidth + 30;                   // allow 30 pixels for radiobuttons    for ( i=0; (n=ourDrawFunctions[i].name) != 0; i++ ) {        <a href="qradiobutton.html">QRadioButton</a> *rb = new <a href="qradiobutton.html">QRadioButton</a>( n, bgroup );<a name="x1192"></a>        rb-&gt;<a href="qwidget.html#setGeometry">setGeometry</a>( 10, i*30+10, maxwidth, 30 );        maxheight += 30;        if ( i == 0 )<a name="x1183"></a>            rb-&gt;<a href="qradiobutton.html#setChecked">setChecked</a>( TRUE );    }    maxheight += 10;                            // maxheight is now 10 pixels upper margin                                                // plus number_of_drawfunctions * 30                                                // plus 10 pixels lower margin    drawindex = 0;                              // draw first thing    maxindex  = i;    maxwidth += 20;                             // add some margin, this results in the                                                // final width of bgroup    bgroup-&gt;<a href="qwidget.html#resize">resize</a>( maxwidth, maxheight );      // resize bgroup to its final size                                                // when no printersupport is provided// If -- at compile time -- printer support will be disabled,// we won't set up printing functionality.#ifndef QT_NO_PRINTER    printer = new <a href="qprinter.html">QPrinter</a>;    // Create and setup the print button    print = new <a href="qpushbutton.html">QPushButton</a>( "Print...", bgroup );<a name="x1182"></a>    print-&gt;<a href="qwidget.html#resize">resize</a>( 80, 30 );<a name="x1194"></a><a name="x1181"></a>    print-&gt;<a href="qwidget.html#move">move</a>( maxwidth/2 - print-&gt;<a href="qwidget.html#width">width</a>()/2, maxindex*30+20 );    <a href="qobject.html#connect">connect</a>( print, SIGNAL(<a href="qbutton.html#clicked">clicked</a>()), SLOT(printIt()) );    // Resize bgroup to its final size when printersupport is given.<a name="x1195"></a><a name="x1186"></a>    bgroup-&gt;<a href="qwidget.html#resize">resize</a>( maxwidth, print-&gt;<a href="qwidget.html#y">y</a>()+print-&gt;<a href="qwidget.html#height">height</a>()+10 );#endif    <a href="qwidget.html#resize">resize</a>( 640,300 );}//// Clean up.//DrawView::~DrawView(){#ifndef QT_NO_PRINTER    delete printer;#endif}//// Called when a radio button is clicked.//void <a name="f378"></a>DrawView::updateIt( int index ){    if ( index &lt; maxindex ) {        drawindex = index;        <a href="qwidget.html#update">update</a>();    }}//// Calls the drawing function as specified by the radio buttons.//void <a name="f379"></a>DrawView::drawIt( <a href="qpainter.html">QPainter</a> *p ){    (*ourDrawFunctions[drawindex].f)(p);}//// Called when the print button is clicked.//void <a name="f380"></a>DrawView::printIt(){<a name="x1180"></a>    if ( printer-&gt;<a href="qprinter.html#setup">setup</a>( this ) ) {        <a href="qpainter.html">QPainter</a> paint( printer );        drawIt( &amp;paint );    }}//// Called when the widget needs to be updated.//<a name="x1188"></a>void DrawView::<a href="qwidget.html#paintEvent">paintEvent</a>( <a href="qpaintevent.html">QPaintEvent</a> * ){    <a href="qpainter.html">QPainter</a> paint( this );    drawIt( &amp;paint );}//// Called when the widget has been resized.// Moves the button group to the upper right corner// of the widget.<a name="x1190"></a>void DrawView::<a href="qwidget.html#resizeEvent">resizeEvent</a>( <a href="qresizeevent.html">QResizeEvent</a> * ){<a name="x1187"></a>    bgroup-&gt;<a href="qwidget.html#move">move</a>( <a href="qwidget.html#width">width</a>()-bgroup-&gt;<a href="qwidget.html#width">width</a>(), 0 );}//// Create and display our widget.//#include "drawdemo.moc"int main( int argc, char **argv ){    <a href="qapplication.html">QApplication</a> app( argc, argv );    DrawView   draw;    app.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &amp;draw );    draw.<a href="qwidget.html#setCaption">setCaption</a>("Qt Example - Drawdemo");    draw.<a href="qwidget.html#show">show</a>();    return app.<a href="qapplication.html#exec">exec</a>();}</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 &copy; 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 + =
减小字号Ctrl + -
显示快捷键?