1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
<!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 - drawlines/connect.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></a>
</div>
</table>
<h1 align=center>Connect the Points</h1><br clear="all">
This example shows very simple mouse-based user interaction and
painting without any world transform matrix or other advanced
features. Run the program, click the button, move the mouse,
release the button, and watch the lines get drawn.
<hr>
Implementation:
<pre>/****************************************************************************
** $Id: qt/examples/drawlines/connect.cpp 2.3.1 edited 2001-01-26 $
**
** 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 name="qwidget.h"></a><a href="qwidget-h.html">qwidget.h</a>>
#include <<a name="qpainter.h"></a><a href="qpainter-h.html">qpainter.h</a>>
#include <<a name="qapplication.h"></a><a href="qapplication-h.html">qapplication.h</a>>
#include <stdlib.h>
const int MAXPOINTS = 2000; // maximum number of points
const int MAXCOLORS = 40;
//
// ConnectWidget - draws connected lines
//
class ConnectWidget : public QWidget
{
public:
ConnectWidget( <a name="QWidget"></a><a href="qwidget.html">QWidget</a> *parent=0, const char *name=0 );
~ConnectWidget();
protected:
void paintEvent( <a name="QPaintEvent"></a><a href="qpaintevent.html">QPaintEvent</a> * );
void mousePressEvent( <a name="QMouseEvent"></a><a href="qmouseevent.html">QMouseEvent</a> *);
void mouseReleaseEvent( <a href="qmouseevent.html">QMouseEvent</a> *);
void mouseMoveEvent( <a href="qmouseevent.html">QMouseEvent</a> *);
private:
<a name="QPoint"></a><a href="qpoint.html">QPoint</a> *points; // point array
<a name="QColor"></a><a href="qcolor.html">QColor</a> *colors; // color array
int count; // count = number of points
bool down; // TRUE if mouse down
};
//
// Constructs a ConnectWidget.
//
ConnectWidget::ConnectWidget( <a href="qwidget.html">QWidget</a> *parent, const char *name )
: <a href="qwidget.html">QWidget</a>( parent, name, WNorthWestGravity )
{
<a name="setBackgroundColor"></a><a href="qwidget.html#721b7d">setBackgroundColor</a>( white ); // white background
count = 0;
down = FALSE;
points = new <a href="qpoint.html">QPoint</a>[MAXPOINTS];
colors = new <a href="qcolor.html">QColor</a>[MAXCOLORS];
for ( int i=0; i<MAXCOLORS; i++ ) // init color array
colors[i] = QColor( rand()&255, rand()&255, rand()&255 );
}
ConnectWidget::~ConnectWidget()
{
delete[] points; // cleanup
delete[] colors;
}
//
// Handles paint events for the connect widget.
//
void <a name="122"></a>ConnectWidget::paintEvent( <a href="qpaintevent.html">QPaintEvent</a> * )
{
<a name="QPainter"></a><a href="qpainter.html">QPainter</a> paint( this );
for ( int i=0; i<count-1; i++ ) { // connect all points
for ( int j=i+1; j<count; j++ ) {
paint.<a name="setPen"></a><a href="qpainter.html#0a6aff">setPen</a>( colors[rand()%MAXCOLORS] ); // set random pen color
paint.<a name="drawLine"></a><a href="qpainter.html#ffde14">drawLine</a>( points[i], points[j] ); // draw line
}
}
}
//
// Handles mouse press events for the connect widget.
//
void <a name="123"></a>ConnectWidget::mousePressEvent( <a href="qmouseevent.html">QMouseEvent</a> * )
{
down = TRUE;
count = 0; // start recording points
<a name="erase"></a><a href="qwidget.html#4f563f">erase</a>(); // erase widget contents
}
//
// Handles mouse release events for the connect widget.
//
void <a name="124"></a>ConnectWidget::mouseReleaseEvent( <a href="qmouseevent.html">QMouseEvent</a> * )
{
down = FALSE; // done recording points
<a name="update"></a><a href="qwidget.html#4a97ef">update</a>(); // draw the lines
}
//
// Handles mouse move events for the connect widget.
//
void <a name="125"></a>ConnectWidget::mouseMoveEvent( <a href="qmouseevent.html">QMouseEvent</a> *e )
{
if ( down && count < MAXPOINTS ) {
<a href="qpainter.html">QPainter</a> paint( this );
points[count++] = e-><a name="pos"></a><a href="qmouseevent.html#ac025d">pos</a>(); // add point
paint.<a name="drawPoint"></a><a href="qpainter.html#036310">drawPoint</a>( e-><a href="qmouseevent.html#ac025d">pos</a>() ); // plot point
}
}
//
// Create and display a ConnectWidget.
//
int main( int argc, char **argv )
{
<a name="QApplication"></a><a href="qapplication.html">QApplication</a> a( argc, argv );
ConnectWidget connect;
a.<a name="setMainWidget"></a><a href="qapplication.html#11c921">setMainWidget</a>( &connect );
connect.<a name="show"></a><a href="qwidget.html#ccc8f7">show</a>();
return a.<a name="exec"></a><a href="qapplication.html#7076af">exec</a>();
}
</pre>
<p><address><hr><div align="center">
<table width="100%" cellspacing="0" border="0"><tr>
<td>Copyright 2000 Trolltech<td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a>
<td align="right"><div align="right">Qt version 2.3.1</div>
</table></div></address></body></html>
|