Drawing Points and connecting them with a line



  • I have made this program which is a basic paint application, I'm trying to edit it now so when the mouse is clicked, a point(like a point on a graph) is made at the spot where the mouse has been clicked and after 3 or 4 points are made they are connected using a line. I'm struggling with the initial bit at the moment just getting the program to create a point on mouse press is proving difficult, any help would be appreciated. Here is my working version of the paint app.
    header file
    @#ifndef LINE_WIDGET
    #define LINE_WIDGET

    #include <QWidget>
    #include <QtGui>

    #define PIXEL_POINTS 100000

    #define PAGE_WIDTH 0
    #define PAGE_HEIGHT 0

    //Class
    class lineWidget : public QWidget
    {
    Q_OBJECT

    public:
    //model
    QImage theImage = new QImage(10000, 10000, QImage::Format_RGB32);
    lineWidget(QWidget
    parent = 0);
    //routines
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void paintEvent(QPaintEvent *event);

    private:
    int gx[PIXEL_POINTS], gy[PIXEL_POINTS];
    int gi;

    };

    #endif // LINE_WIDGET@

    cpp file
    @#include <QtGui>
    #include "lineWidget.h"

    lineWidget::lineWidget(QWidget *parent):
    QWidget(parent)
    {
    gi=0;
    //set the image to white
    QColor whiteColour(255, 255, 255);
    theImage->fill(whiteColour.rgba());

    }

    void lineWidget::paintEvent(QPaintEvent *event)
    {
    QPainter painter(this);
    //set image to screen
    QPointF imageOrigin(0.0, 0.0);
    painter.drawImage(imageOrigin, *theImage);
    int i;
    painter.setPen(QPen(Qt::black, 5 ));
    for ( i = 0 ; i < gi-1 ; ++ i ){
    if ( gy[i] > PAGE_WIDTH + PAGE_HEIGHT )
    painter.drawPoint(QPointF( gx[i], gy[i]));

    }
    }
    void lineWidget::mouseMoveEvent( QMouseEvent *event){
    gx[gi] = (event->x());
    gy[gi] = (event->y());
    if ( gi < PIXEL_POINTS - 1)
    ++gi;
    repaint();
    }
    void lineWidget::mousePressEvent(QMouseEvent *event) {
    gx[gi] = (event->x());
    gy[gi] = (event->y());

    } @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.