alex
alex

Reputation: 9

How to save drawn points on a QWidget in a list?

I'm new to Qtcreator and I'm trying to draw some points on a widget. With the code I have it just works fine and I can draw as many points as I want the problem is though that if I cover my widget with another window and then show my widget again than I only get the last point I drew. Can some please tell me how I can solve this problem so I can always have all the points there that I drew.

Thanks in advance

void MainWindow::mousePressEvent(QMouseEvent *e)
{
    point=e->pos();
    update();
}
void MainWindow::paintEvent(QPaintEvent *e)
{
    setAttribute(Qt::WA_OpaquePaintEvent);
    QPainter painter(this);
    QPen linepen(Qt::red);
    linepen.setCapStyle(Qt::RoundCap);
    linepen.setWidth(30);
    painter.setRenderHint(QPainter::Antialiasing,true);
    painter.setPen(linepen);
    painter.drawPoint(point);
}

Upvotes: 0

Views: 351

Answers (1)

Marek R
Marek R

Reputation: 37607

void MainWindow::mousePressEvent(QMouseEvent *e)
{
    // points is a "QList<QPoint> points;"
    points.append(e->pos());
    update();
}

void MainWindow::paintEvent(QPaintEvent *e)
{
    setAttribute(Qt::WA_OpaquePaintEvent);
    QPainter painter(this);
    QPen linepen(Qt::red);
    linepen.setCapStyle(Qt::RoundCap);
    linepen.setWidth(30);
    painter.setRenderHint(QPainter::Antialiasing,true);
    painter.setPen(linepen);
    for (auto point : points) {
        painter.drawPoint(point);
    }
}

Upvotes: 3

Related Questions