Qt画家不更新

Qt Painter don't update

本文关键字:更新 画家 Qt      更新时间:2023-10-16

我需要绘制简单的矩形,但当我调用update()函数时,它不做任何事情。下面是我的代码:

...//this is called when button is pressed, when application loads nothing should be drawn.
    draw = true;
    update();
}
void MainWindow::paintEvent(QPaintEvent *e){
    QPainter painter(this);
    QLinearGradient lg(QPoint(20, 80), QPoint(215, 390));
    lg.setColorAt(0, Qt::white);
    lg.setColorAt(1, Qt::blue);
    QBrush brush(lg);
    painter.setPen(Qt::black);
    painter.setBrush(brush);
    if(draw == true){
    painter.drawRect(20, 80, 195, 300);
}
}

您应该尝试添加

e->accept()