返回介绍

Connect the Points

发布于 2019-10-04 14:57:53 字数 3859 浏览 964 评论 0 收藏 0

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.


Implementation:

/****************************************************************************
** $Id:  qt/connect.cpp   3.0.5   edited Oct 12 2001 $
**
** 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 <qwidget.h>
#include <qpainter.h>
#include <qapplication.h>
#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( QWidget *parent=0, const char *name=0 );
   ~ConnectWidget();
protected:
    void        paintEvent( QPaintEvent * );
    void        mousePressEvent( QMouseEvent *);
    void        mouseReleaseEvent( QMouseEvent *);
    void        mouseMoveEvent( QMouseEvent *);
private:
    QPoint     *points;                         // point array
    QColor     *colors;                         // color array
    int         count;                          // count = number of points
    bool        down;                           // TRUE if mouse down
};


//
// Constructs a ConnectWidget.
//

ConnectWidget::ConnectWidget( QWidget *parent, const char *name )
    : QWidget( parent, name, WStaticContents )
{
    setBackgroundColor( white );                // white background
    count = 0;
    down = FALSE;
    points = new QPoint[MAXPOINTS];
    colors = new QColor[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 ConnectWidget::paintEvent( QPaintEvent * )
{
    QPainter paint( this );
    for ( int i=0; i<count-1; i++ ) {           // connect all points
        for ( int j=i+1; j<count; j++ ) {
            paint.setPen( colors[rand()%MAXCOLORS] ); // set random pen color
            paint.drawLine( points[i], points[j] ); // draw line
        }
    }
}


//
// Handles mouse press events for the connect widget.
//

void ConnectWidget::mousePressEvent( QMouseEvent * )
{
    down = TRUE;
    count = 0;                                  // start recording points
    erase();                                    // erase widget contents
}


//
// Handles mouse release events for the connect widget.
//

void ConnectWidget::mouseReleaseEvent( QMouseEvent * )
{
    down = FALSE;                               // done recording points
    update();                                   // draw the lines
}


//
// Handles mouse move events for the connect widget.
//

void ConnectWidget::mouseMoveEvent( QMouseEvent *e )
{
    if ( down && count < MAXPOINTS ) {
        QPainter paint( this );
        points[count++] = e->pos();             // add point
        paint.drawPoint( e->pos() );            // plot point
    }
}


//
// Create and display a ConnectWidget.
//

int main( int argc, char **argv )
{
    QApplication a( argc, argv );
    ConnectWidget connect;
#ifndef QT_NO_WIDGET_TOPEXTRA   // for Qt/Embedded minimal build
    connect.setCaption( "Qt Example - Draw lines");
#endif
    a.setMainWidget( &connect );
    connect.show();
    return a.exec();
}

See also Examples.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文