1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
---|
2 | <!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/drawlines/drawlines.doc:5 -->
|
---|
3 | <html>
|
---|
4 | <head>
|
---|
5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
6 | <title>Connect the Points</title>
|
---|
7 | <style type="text/css"><!--
|
---|
8 | fn { margin-left: 1cm; text-indent: -1cm; }
|
---|
9 | a:link { color: #004faf; text-decoration: none }
|
---|
10 | a:visited { color: #672967; text-decoration: none }
|
---|
11 | body { background: #ffffff; color: black; }
|
---|
12 | --></style>
|
---|
13 | </head>
|
---|
14 | <body>
|
---|
15 |
|
---|
16 | <table border="0" cellpadding="0" cellspacing="0" width="100%">
|
---|
17 | <tr bgcolor="#E5E5E5">
|
---|
18 | <td valign=center>
|
---|
19 | <a href="index.html">
|
---|
20 | <font color="#004faf">Home</font></a>
|
---|
21 | | <a href="classes.html">
|
---|
22 | <font color="#004faf">All Classes</font></a>
|
---|
23 | | <a href="mainclasses.html">
|
---|
24 | <font color="#004faf">Main Classes</font></a>
|
---|
25 | | <a href="annotated.html">
|
---|
26 | <font color="#004faf">Annotated</font></a>
|
---|
27 | | <a href="groups.html">
|
---|
28 | <font color="#004faf">Grouped Classes</font></a>
|
---|
29 | | <a href="functions.html">
|
---|
30 | <font color="#004faf">Functions</font></a>
|
---|
31 | </td>
|
---|
32 | <td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Connect the Points</h1>
|
---|
33 |
|
---|
34 |
|
---|
35 | <p>
|
---|
36 | This example shows very simple mouse-based user interaction and
|
---|
37 | painting without any world transform matrix or other advanced
|
---|
38 | features. Run the program, click the button, move the mouse,
|
---|
39 | release the button, and watch the lines get drawn.
|
---|
40 | <p> <hr>
|
---|
41 | <p> Implementation:
|
---|
42 | <p> <pre>/****************************************************************************
|
---|
43 | ** $Id: drawlines-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
44 | **
|
---|
45 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
46 | **
|
---|
47 | ** This file is part of an example program for Qt. This example
|
---|
48 | ** program may be used, distributed and modified without limitation.
|
---|
49 | **
|
---|
50 | *****************************************************************************/
|
---|
51 |
|
---|
52 | #include <<a href="qwidget-h.html">qwidget.h</a>>
|
---|
53 | #include <<a href="qpainter-h.html">qpainter.h</a>>
|
---|
54 | #include <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
55 | #include <stdlib.h>
|
---|
56 |
|
---|
57 |
|
---|
58 | const int MAXPOINTS = 2000; // maximum number of points
|
---|
59 | const int MAXCOLORS = 40;
|
---|
60 |
|
---|
61 |
|
---|
62 | //
|
---|
63 | // ConnectWidget - draws connected lines
|
---|
64 | //
|
---|
65 |
|
---|
66 | class ConnectWidget : public <a href="qwidget.html">QWidget</a>
|
---|
67 | {
|
---|
68 | public:
|
---|
69 | ConnectWidget( <a href="qwidget.html">QWidget</a> *parent=0, const char *name=0 );
|
---|
70 | ~ConnectWidget();
|
---|
71 | protected:
|
---|
72 | void paintEvent( <a href="qpaintevent.html">QPaintEvent</a> * );
|
---|
73 | void mousePressEvent( <a href="qmouseevent.html">QMouseEvent</a> *);
|
---|
74 | void mouseReleaseEvent( <a href="qmouseevent.html">QMouseEvent</a> *);
|
---|
75 | void mouseMoveEvent( <a href="qmouseevent.html">QMouseEvent</a> *);
|
---|
76 | private:
|
---|
77 | <a href="qpoint.html">QPoint</a> *points; // point array
|
---|
78 | <a href="qcolor.html">QColor</a> *colors; // color array
|
---|
79 | int count; // count = number of points
|
---|
80 | bool down; // TRUE if mouse down
|
---|
81 | };
|
---|
82 |
|
---|
83 |
|
---|
84 | //
|
---|
85 | // Constructs a ConnectWidget.
|
---|
86 | //
|
---|
87 |
|
---|
88 | <a name="f475"></a>ConnectWidget::ConnectWidget( <a href="qwidget.html">QWidget</a> *parent, const char *name )
|
---|
89 | : <a href="qwidget.html">QWidget</a>( parent, name, WStaticContents )
|
---|
90 | {
|
---|
91 | <a href="qwidget.html#setBackgroundColor">setBackgroundColor</a>( white ); // white background
|
---|
92 | count = 0;
|
---|
93 | down = FALSE;
|
---|
94 | points = new <a href="qpoint.html">QPoint</a>[MAXPOINTS];
|
---|
95 | colors = new <a href="qcolor.html">QColor</a>[MAXCOLORS];
|
---|
96 | for ( int i=0; i<MAXCOLORS; i++ ) // init color array
|
---|
97 | colors[i] = QColor( rand()&255, rand()&255, rand()&255 );
|
---|
98 | }
|
---|
99 |
|
---|
100 | ConnectWidget::~ConnectWidget()
|
---|
101 | {
|
---|
102 | delete[] points; // cleanup
|
---|
103 | delete[] colors;
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | //
|
---|
108 | // Handles paint events for the connect widget.
|
---|
109 | //
|
---|
110 |
|
---|
111 | <a name="x1661"></a>void ConnectWidget::<a href="qwidget.html#paintEvent">paintEvent</a>( <a href="qpaintevent.html">QPaintEvent</a> * )
|
---|
112 | {
|
---|
113 | <a href="qpainter.html">QPainter</a> paint( this );
|
---|
114 | for ( int i=0; i<count-1; i++ ) { // connect all points
|
---|
115 | for ( int j=i+1; j<count; j++ ) {
|
---|
116 | paint.<a href="qpainter.html#setPen">setPen</a>( colors[rand()%MAXCOLORS] ); // set random pen color
|
---|
117 | <a name="x1655"></a> paint.<a href="qpainter.html#drawLine">drawLine</a>( points[i], points[j] ); // draw line
|
---|
118 | }
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 | //
|
---|
124 | // Handles mouse press events for the connect widget.
|
---|
125 | //
|
---|
126 |
|
---|
127 | <a name="x1659"></a>void ConnectWidget::<a href="qwidget.html#mousePressEvent">mousePressEvent</a>( <a href="qmouseevent.html">QMouseEvent</a> * )
|
---|
128 | {
|
---|
129 | down = TRUE;
|
---|
130 | count = 0; // start recording points
|
---|
131 | <a href="qwidget.html#erase">erase</a>(); // erase widget contents
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 | //
|
---|
136 | // Handles mouse release events for the connect widget.
|
---|
137 | //
|
---|
138 |
|
---|
139 | <a name="x1660"></a>void ConnectWidget::<a href="qwidget.html#mouseReleaseEvent">mouseReleaseEvent</a>( <a href="qmouseevent.html">QMouseEvent</a> * )
|
---|
140 | {
|
---|
141 | down = FALSE; // done recording points
|
---|
142 | <a href="qwidget.html#update">update</a>(); // draw the lines
|
---|
143 | }
|
---|
144 |
|
---|
145 |
|
---|
146 | //
|
---|
147 | // Handles mouse move events for the connect widget.
|
---|
148 | //
|
---|
149 |
|
---|
150 | <a name="x1658"></a>void ConnectWidget::<a href="qwidget.html#mouseMoveEvent">mouseMoveEvent</a>( <a href="qmouseevent.html">QMouseEvent</a> *e )
|
---|
151 | {
|
---|
152 | if ( down && count < MAXPOINTS ) {
|
---|
153 | <a href="qpainter.html">QPainter</a> paint( this );
|
---|
154 | <a name="x1654"></a> points[count++] = e-><a href="qmouseevent.html#pos">pos</a>(); // add point
|
---|
155 | <a name="x1656"></a> paint.<a href="qpainter.html#drawPoint">drawPoint</a>( e-><a href="qmouseevent.html#pos">pos</a>() ); // plot point
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 |
|
---|
160 | //
|
---|
161 | // Create and display a ConnectWidget.
|
---|
162 | //
|
---|
163 |
|
---|
164 | int main( int argc, char **argv )
|
---|
165 | {
|
---|
166 | <a href="qapplication.html">QApplication</a> a( argc, argv );
|
---|
167 | ConnectWidget connect;
|
---|
168 | #ifndef QT_NO_WIDGET_TOPEXTRA // for Qt/Embedded minimal build
|
---|
169 | connect.<a href="qwidget.html#setCaption">setCaption</a>( "Qt Example - Draw lines");
|
---|
170 | #endif
|
---|
171 | a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &connect );
|
---|
172 | connect.<a href="qwidget.html#show">show</a>();
|
---|
173 | return a.<a href="qapplication.html#exec">exec</a>();
|
---|
174 | }
|
---|
175 | </pre>
|
---|
176 |
|
---|
177 | <p>See also <a href="examples.html">Examples</a>.
|
---|
178 |
|
---|
179 | <!-- eof -->
|
---|
180 | <p><address><hr><div align=center>
|
---|
181 | <table width=100% cellspacing=0 border=0><tr>
|
---|
182 | <td>Copyright © 2007
|
---|
183 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
184 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
185 | </table></div></address></body>
|
---|
186 | </html>
|
---|