PDA

View Full Version : QPainter - drawing new graphics without removing old stuff



rickysts
14th April 2008, 15:39
Folks,

A trivial question. How do I draw stuff on the canvas without erasing the old stuff?

Consider this code snippet for drawing 200 random points on a 320x240 window:

void dlg::timerEvent(QTimerEvent *event)
{
if (count < 200) {
count=count+1;
x = rand() % 320;
y = rand() % 240;
update();
} else {
timer.stop();
}
}

void dlg::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.drawPoint(x,y);
update();
}

It'd draw one point, erase the screen and then draw the next point. I don't want that. I want the old points to stay. How do I do this?

Thanks,

Ricky.

wysota
14th April 2008, 15:54
Keep a list of points and draw them all.

rickysts
14th April 2008, 16:01
I want a delay of 0.5 seconds before drawing each point, hence the timer. Not sure how this'd work out if I had the list of all 200 points in an array... code snippet anyone?


Keep a list of points and draw them all.

aamer4yu
14th April 2008, 16:21
something like -


QTimer::singleShot(500,this, onTimer());

void Dlg::OnTimer()
{
m_index_of_point++;
drawPoint(m_pointList[m_index_of_point],x,y); // move the index to next point and draw it at x,y positiom.
}


Hope u get the idea :)

wysota
14th April 2008, 16:39
I want a delay of 0.5 seconds before drawing each point, hence the timer. Not sure how this'd work out if I had the list of all 200 points in an array... code snippet anyone?


void X::timerEvent(QTimerEvent *te){
QPoint pt(qRand()%width(), qRand()%height());
points << pt; // assuming "points" is a QList<QPoint> class member
update();
}

void X::paintEvent(QPaintEvent *pe){
QPainter p(this);
QPen pe = p.pen();
pe.setWidth(2);
p.setPen(pe);
foreach(const QPoint &pt, points){
p.drawPoint(pt);
}
}