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#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 }
Edited: Use ``` (3 backticks) instead - p3c0
-
I have not tried to run your code but generally what this involves is on the current line that you are previewing you have to do pretty much the following
first time - draw it but remember/store what line you drew
detect if move, if moved XOR draw the saved line (erases it), then draw the new line and save it.So you sort of need to keep a temp history of the last line you drew before releasing the mouse. If your mouse moves then you must know if you drew a preview line and XOR draw it.
I have not used QPainter yet so I'm not able to give you the calls to make but the basic concept is if you drew a line in red, you need to undo that line either by redrawing it in your background color or a color that is the XOR of red.
-
@SysTech Thanks I understand your idea. But how do I XOR a drawn line?.. Redrawing in background color might not be feasible because there could be other shapes drawn underneath the current drawing object.
-
@simpleProgrammer777
Like I said I have not used QPainter (yet)... so I don't have the calls right handy. Hopefully someone else will chime in.If I get time I will take a look.
One thing I might suggest is to take a look at the Analog Clock example program. This appears to me to do a complete redraw but it's very fast. You might be able to make use of that technique.
-
I have a solution thanks :)