You are not logged in.

1

Tuesday, March 31st 2015, 3:50pm

Drawing a line dynamically on QtWidget

Hi, Here is a code to draw a line on widget. Problem is it's keeping the previously drawn line and on each mouse move it's drawing again and again. I want to draw like mspaint i.e only on mouse release event it will finalize the line drawing (otherwise just preview of lines). One idea I thought about is to delete the preview lines on each mouse move, other is to draw on some temporary view or something and on mouse release finalize it. But having difficulty finding a proper way how to do it.
Any help is highly appreciated. Thanks

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "paintwidget.h"
#include "ui_paintwidget.h"

#include <QtGui>

paintWidget::paintWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::paintWidget)
{
    ui->setupUi(this);

    m_nInitialX = 0;
    m_nInitialY = 0;
    m_nFinalX = 0;
    m_nFinalY = 0;
    m_nPTargetPixmap = 0;
    m_nPTargetPixmap = new QPixmap(400,400);
    m_nbMousePressed = false;
}

paintWidget::~paintWidget()
{
    delete ui;
}

void paintWidget::mousePressEvent(QMouseEvent* event)
{
    m_nbMousePressed = true;
    m_nInitialX = event->pos().x();
    m_nInitialY = event->pos().y();
}

void paintWidget::mouseReleaseEvent(QMouseEvent *event)
{
    m_nbMousePressed = false;
    //update();
}

void paintWidget::paintEvent(QPaintEvent *e)
{
    if(m_nbMousePressed)
    {
        QPainter PixmapPainter(m_nPTargetPixmap);
        QPen pen(Qt::green);
        PixmapPainter.setPen(pen);
        //PixmapPainter.drawLine(m_nInitialX, m_nInitialY, m_nFinalX, m_nFinalY);
    }
    QPainter painter(this);
    painter.drawPixmap(0, 0, *m_nPTargetPixmap);
}

void paintWidget::mouseMoveEvent(QMouseEvent *event)
{
    if (event->type() == QEvent::MouseMove)
    {
        QPainter PixmapPainter(m_nPTargetPixmap);
        QPen pen(Qt::black);
        PixmapPainter.setPen(pen);
        PixmapPainter.drawLine(m_nInitialX, m_nInitialY, m_nFinalX, m_nFinalY);
        update(); // update your view
        m_nFinalX = event->pos().x();
        m_nFinalY = event->pos().y();
    }
    update(); // update your view
}


#header file

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#ifndef PAINTWIDGET_H
#define PAINTWIDGET_H


#include <QWidget>


namespace Ui {
class paintWidget;
}


class paintWidget : public QWidget
{
    Q_OBJECT


public:
    explicit paintWidget(QWidget *parent = 0);
    ~paintWidget();
    int m_nbMousePressed;
    int m_nInitialX;
    int m_nInitialY;
    int m_nFinalX;
    int m_nFinalY;
    //int m_nPTargetPixmap;
    QPixmap *m_nPTargetPixmap;


protected:
    void mousePressEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void paintEvent(QPaintEvent *e);
    void mouseMoveEvent(QMouseEvent *event);
private:
    Ui::paintWidget *ui;
};


#endif // PAINTWIDGET_H

2

Friday, September 18th 2015, 9:06am

define a strcuture like : struct MyLine { int initx; int inity; int endx; int endy; };

then use QList<MyLine> to store all lines, stroe one line in function mouseReleaseEvent, and repaint all lines in the mouseMoveEvent function.

before repainting, make sure to clear the m_nPTargetPixmap;

Similar threads