⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 life-main-cpp.html

📁 qtopiaphone英文帮助,用于初学者和开发人员,初学者可以用来学习,开发人员可以用来资料查询.
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Qt Toolkit - life/main.cpp example file</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: white; color: black; }--></style></head><body bgcolor="#ffffff"><table width="100%"><tr><td><a href="index.html"><img width="100" height="100" src="qtlogo.png"alt="Home" border="0"><img width="100"height="100" src="face.png" alt="Home" border="0"></a><td valign="top"><div align="right"><img src="dochead.png" width="472" height="27"><br><a href="classes.html"><b>Classes</b></a>- <a href="annotated.html">Annotated</a>- <a href="hierarchy.html">Tree</a>- <a href="functions.html">Functions</a>- <a href="index.html">Home</a>- <a href="topicals.html"><b>Structure</b>  <font face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular" align="center" size=32>Qte</font></a></div></table><h1 align=center>Conway's Game of Life</h1><br clear="all">  ???  <hr>  Header file: <pre>/****************************************************************************** &#36;Id&#58; qt/examples/life/life.h   2.3.8   edited 2004-05-12 $**** 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.*******************************************************************************/#ifndef LIFE_H#define LIFE_H#include &lt;<a href="qframe-h.html">qframe.h</a>&gt;class LifeWidget : public QFrame{    Q_OBJECTpublic:    LifeWidget( int s = 10, QWidget *parent = 0, const char *name = 0 );    void        setPoint( int i, int j );    int         maxCol() { return maxi; }    int         maxRow() { return maxj; }public slots:    void        nextGeneration();    void        clear();protected:    virtual void paintEvent( <a href="qpaintevent.html">QPaintEvent</a> * );    virtual void mouseMoveEvent( <a href="qmouseevent.html">QMouseEvent</a> * );    virtual void mousePressEvent( <a href="qmouseevent.html">QMouseEvent</a> * );    virtual void resizeEvent( <a href="qresizeevent.html">QResizeEvent</a> * );    void         mouseHandle( const QPoint &amp;pos );private:    enum { MAXSIZE = 50, MINSIZE = 10, BORDER = 5 };    bool        cells[2][MAXSIZE + 2][MAXSIZE + 2];    int         current;    int         maxi, maxj;    int pos2index( int x )    {        return ( x - BORDER ) / SCALE + 1;    }    int index2pos( int i )    {        return  ( i - 1 ) * SCALE + BORDER;    }    int SCALE;};#endif // LIFE_H</pre>  <hr>  Implementation: <pre>/****************************************************************************** &#36;Id&#58; qt/examples/life/life.cpp   2.3.8   edited 2004-05-12 $**** 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 "life.h"#include &lt;<a href="qpainter-h.html">qpainter.h</a>&gt;#include &lt;<a href="qdrawutil-h.html">qdrawutil.h</a>&gt;#include &lt;<a href="qcheckbox-h.html">qcheckbox.h</a>&gt;#include &lt;<a href="qevent-h.html">qevent.h</a>&gt;#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;// The main game of life widgetLifeWidget::LifeWidget( int s, QWidget *parent, const char *name )    : <a href="qframe.html">QFrame</a>( parent, name ){    SCALE = s;    maxi = maxj = 50;    <a href="qwidget.html#c0b5fb">setMinimumSize</a>( MINSIZE * SCALE + 2 * BORDER,                   MINSIZE * SCALE + 2 * BORDER );    <a href="qwidget.html#30296d">setMaximumSize</a>( MAXSIZE * SCALE + 2 * BORDER,                   MAXSIZE * SCALE + 2 * BORDER );    <a href="qwidget.html#baa891">setSizeIncrement</a>( SCALE, SCALE);    <a href=#148>clear</a>();    <a href="qwidget.html#ff9d07">resize</a>( maxi * SCALE + 2 * BORDER , maxj * SCALE + 2 * BORDER );}void <a name="148"></a>LifeWidget::clear(){    current = 0;    for ( int t = 0; t &lt; 2; t++ )        for ( int i = 0; i &lt; MAXSIZE + 2; i++ )            for ( int j = 0; j &lt; MAXSIZE + 2; j++ )                cells[t][i][j] = FALSE;    <a href="qwidget.html#138c7b">repaint</a>();}// We assume that the size will never be beyond the maximum size set// this is not in general TRUE, but in practice it's good enough for// this programvoid <a name="149"></a>LifeWidget::resizeEvent( <a href="qresizeevent.html">QResizeEvent</a> * e ){    maxi = (e-&gt;size().width()  - 2 * BORDER) / SCALE;    maxj = (e-&gt;size().height() - 2 * BORDER) / SCALE;}void <a name="150"></a>LifeWidget::setPoint( int i, int j ){    if ( i &lt; 1 || i &gt; maxi || j &lt; 1 || j &gt; maxi )        return;    cells[current][i][j] = TRUE;    <a href="qwidget.html#138c7b">repaint</a>( index2pos(i), index2pos(j), SCALE, SCALE, FALSE );}void <a name="151"></a>LifeWidget::mouseHandle( const QPoint &amp;pos ){    int i = pos2index( pos.x() );    int j = pos2index( pos.y() );    <a href=#150>setPoint</a>( i, j );}void <a name="152"></a>LifeWidget::mouseMoveEvent( <a href="qmouseevent.html">QMouseEvent</a> *e ){    <a href=#151>mouseHandle</a>( e-&gt;pos() );}void <a name="153"></a>LifeWidget::mousePressEvent( <a href="qmouseevent.html">QMouseEvent</a> *e ){    if ( e-&gt;button() == QMouseEvent::LeftButton )        <a href=#151>mouseHandle</a>( e-&gt;pos() );}void <a name="154"></a>LifeWidget::nextGeneration(){    for ( int i = 1; i &lt;= MAXSIZE; i++ ) {        for ( int j = 1; j &lt;= MAXSIZE; j++ ) {            int t = cells[current][i - 1][j - 1]            + cells[current][i - 1][j]            + cells[current][i - 1][j + 1]            + cells[current][i][j - 1]            + cells[current][i][j + 1]            + cells[current][i + 1][j - 1]            + cells[current][i + 1][j]            + cells[current][i + 1][j + 1];            cells[!current][i][j] = ( t == 3 ||                                      t == 2 &amp;&amp; cells[current][i][j] );        }    }    current = !current;    <a href="qwidget.html#138c7b">repaint</a>( FALSE );           // repaint without erase}void <a name="155"></a>LifeWidget::paintEvent( <a href="qpaintevent.html">QPaintEvent</a> * e ){    int starti = pos2index( e-&gt;<a href="qpaintevent.html#2d6e18">rect</a>().left() );    int stopi  = pos2index( e-&gt;<a href="qpaintevent.html#2d6e18">rect</a>().right() );    int startj = pos2index( e-&gt;<a href="qpaintevent.html#2d6e18">rect</a>().top() );    int stopj  = pos2index( e-&gt;<a href="qpaintevent.html#2d6e18">rect</a>().bottom() );    if (stopi &gt; maxi)        stopi = maxi;    if (stopj &gt; maxj)        stopj = maxj;    <a href="qpainter.html">QPainter</a> paint( this );    for ( int i = starti; i &lt;= stopi; i++ ) {        for ( int j = startj; j &lt;= stopj; j++ ) {            if ( cells[current][i][j] )                <a href="qpainter.html#b03ffb">qDrawShadePanel</a>( &amp;paint, index2pos( i ), index2pos( j ),                                 SCALE - 1, SCALE - 1, <a href="qwidget.html#d92bf4">colorGroup</a>() );            else if ( cells[!current][i][j] )                paint.<a href="qpainter.html#bd5570">eraseRect</a>( index2pos( i ), index2pos( j ),                                 SCALE - 1, SCALE - 1);        }    }    <a href="qframe.html#0dc534">drawFrame</a>( &amp;paint );}</pre>  <hr>  Main:<pre>/****************************************************************************** &#36;Id&#58; qt/examples/life/main.cpp   2.3.8   edited 2004-05-12 $**** 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 "lifedlg.h"#include &lt;<a name="qapplication.h"></a><a href="qapplication-h.html">qapplication.h</a>&gt;#include &lt;stdlib.h&gt;void usage(){    <a name="qWarning"></a><a href="qapplication.html#290ef4">qWarning</a>( "Usage: life [-scale scale]" );}int main( int argc, char **argv ){    <a name="QApplication"></a><a href="qapplication.html">QApplication</a> a( argc, argv );    int scale = 10;    for ( int i = 1; i &lt; argc; i++ ){        <a name="QString"></a><a href="qstring.html">QString</a> arg = argv[i];        if ( arg == "-scale" )            scale = atoi( argv[++i] );        else {            usage();            exit(1);        }    }    if ( scale &lt; 2 )        scale = 2;    LifeDialog *life = new LifeDialog( scale );    a.<a name="setMainWidget"></a><a href="qapplication.html#7ad759">setMainWidget</a>( life );    life-&gt;<a name="setCaption"></a><a href="qwidget.html#d6a291">setCaption</a>("Qt Example - Life");    life-&gt;<a name="show"></a><a href="qwidget.html#200ee5">show</a>();    return a.<a name="exec"></a><a href="qapplication.html#84c7bf">exec</a>();}</pre><p><address><hr><div align="center"><table width="100%" cellspacing="0" border="0"><tr><td>Copyright 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -