📄 drawlines-connect-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 - 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> <font face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular" align="center" size=32>Qte</font></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.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 <<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 pointsconst 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#c09181">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="88"></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#0183e4">setPen</a>( colors[rand()%MAXCOLORS] ); // set random pen color paint.<a name="drawLine"></a><a href="qpainter.html#e3a489">drawLine</a>( points[i], points[j] ); // draw line } }}//// Handles mouse press events for the connect widget.//void <a name="89"></a>ConnectWidget::mousePressEvent( <a href="qmouseevent.html">QMouseEvent</a> * ){ down = TRUE; count = 0; // start recording points <a name="erase"></a><a href="qwidget.html#10cf79">erase</a>(); // erase widget contents}//// Handles mouse release events for the connect widget.//void <a name="90"></a>ConnectWidget::mouseReleaseEvent( <a href="qmouseevent.html">QMouseEvent</a> * ){ down = FALSE; // done recording points <a name="update"></a><a href="qwidget.html#a66a88">update</a>(); // draw the lines}//// Handles mouse move events for the connect widget.//void <a name="91"></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#ac6f25">pos</a>(); // add point paint.<a name="drawPoint"></a><a href="qpainter.html#8e172b">drawPoint</a>( e-><a href="qmouseevent.html#ac6f25">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#7ad759">setMainWidget</a>( &connect ); connect.<a name="setCaption"></a><a href="qwidget.html#d6a291">setCaption</a>("Qt Example - Draw lines"); connect.<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 + -