简体   繁体   中英

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);
}
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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM