📄 life-main-cpp.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>/****************************************************************************** $Id: 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 <<a href="qframe-h.html">qframe.h</a>>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 &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>/****************************************************************************** $Id: 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 <<a href="qpainter-h.html">qpainter.h</a>>#include <<a href="qdrawutil-h.html">qdrawutil.h</a>>#include <<a href="qcheckbox-h.html">qcheckbox.h</a>>#include <<a href="qevent-h.html">qevent.h</a>>#include <<a href="qapplication-h.html">qapplication.h</a>>// 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 < 2; t++ ) for ( int i = 0; i < MAXSIZE + 2; i++ ) for ( int j = 0; j < 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->size().width() - 2 * BORDER) / SCALE; maxj = (e->size().height() - 2 * BORDER) / SCALE;}void <a name="150"></a>LifeWidget::setPoint( int i, int j ){ if ( i < 1 || i > maxi || j < 1 || j > 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 &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->pos() );}void <a name="153"></a>LifeWidget::mousePressEvent( <a href="qmouseevent.html">QMouseEvent</a> *e ){ if ( e->button() == QMouseEvent::LeftButton ) <a href=#151>mouseHandle</a>( e->pos() );}void <a name="154"></a>LifeWidget::nextGeneration(){ for ( int i = 1; i <= MAXSIZE; i++ ) { for ( int j = 1; j <= 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 && 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-><a href="qpaintevent.html#2d6e18">rect</a>().left() ); int stopi = pos2index( e-><a href="qpaintevent.html#2d6e18">rect</a>().right() ); int startj = pos2index( e-><a href="qpaintevent.html#2d6e18">rect</a>().top() ); int stopj = pos2index( e-><a href="qpaintevent.html#2d6e18">rect</a>().bottom() ); if (stopi > maxi) stopi = maxi; if (stopj > maxj) stopj = maxj; <a href="qpainter.html">QPainter</a> paint( this ); for ( int i = starti; i <= stopi; i++ ) { for ( int j = startj; j <= stopj; j++ ) { if ( cells[current][i][j] ) <a href="qpainter.html#b03ffb">qDrawShadePanel</a>( &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>( &paint );}</pre> <hr> Main:<pre>/****************************************************************************** $Id: 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 <<a name="qapplication.h"></a><a href="qapplication-h.html">qapplication.h</a>>#include <stdlib.h>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 < 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 < 2 ) scale = 2; LifeDialog *life = new LifeDialog( scale ); a.<a name="setMainWidget"></a><a href="qapplication.html#7ad759">setMainWidget</a>( life ); life-><a name="setCaption"></a><a href="qwidget.html#d6a291">setCaption</a>("Qt Example - Life"); life-><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 + -