This is my first time using Qt and I have to make a MSPaint equivalent with Qt. I am however having trouble with painting my lines. I can currently draw a line by clicking somewhere on the screen and releasing somewhere else, however when I draw a second line the previous line is erased. How could I keep the previously painted items when painting another item?
void Canvas::paintEvent(QPaintEvent *pe){
QWidget::paintEvent(pe);
QPainter p(this);
p.drawPicture(0,0,pic);
}
void Canvas::mousePressEvent(QMouseEvent *mp){
start = mp->pos();
}
void Canvas::mouseReleaseEvent(QMouseEvent *mr){
end = mr->pos();
addline();
}
void Canvas::addline()Q_DECL_OVERRIDE{
QPainter p(&pic);
p.drawLine(start,end);
p.end();
this->update();
}
Canvas is a class that derives QWidget, it has 2 QPoint attributes start and end.
Class body:
class Canvas : public QWidget{
Q_OBJECT
private:
QPoint start;
QPoint end;
QPicture pic;
public:
Canvas(){paint = false;setAttribute(Qt::WA_StaticContents);}
void addline();
protected:
void paintEvent(QPaintEvent *);
void mousePressEvent( QMouseEvent * );
//void mouseMoveEvent( QMouseEvent * );
void mouseReleaseEvent( QMouseEvent * );
};
pic
is his buffer, the painter should not affect the buffer data. This doesn't mean keeping the painter is a bad idea. – Unreadablepic
? Show us the body of your widget class. – Unreadablepic
would beQPaintDevice
which is whatQPainter
isn't being drawn to. Hence my theory that it's a scope issue. – Griffenpic
has class scope. It should persist between different calls. From the code posted so far I see nothing that would clearpic
. – Unreadablepic
's reimplementedpaintEngine()
function. Until we get more info, we can't help further. – GriffenQPicture
? Try changing that to aQPixmap
– Unreadable