Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Drawing Points and connecting them with a line
Forum Updated to NodeBB v4.3 + New Features

Drawing Points and connecting them with a line

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 2.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TZHF
    wrote on last edited by
    #1

    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());

    } @

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved