我正在使用 paintEvent(QPaintEvent * event)
画几条线.稍后,如果我想绘制新线,则不会清除/删除先前绘制的线。我怎么能通过删除/清除旧线来只绘制新线。是否有任何属性(property)可以清除先前绘制的线条。请告诉我。
void QGraphWidget::paintEvent(QPaintEvent * event)
{
const QRect & rect = event->rect();
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
drawLines(painter, rect);//calling painter.drawLine() inside
}
谢谢...
最佳答案
在我看来,正确的方法是在绘制新线之前清除该区域。这可以通过使用 Stephen Chu 提出的 autoFillBackground 属性来实现,也可以通过手动调用 QPainter::eraseRect()
来实现。在 QGraphWidget::paintEvent
中绘制线条之前方法。
void QGraphWidget::paintEvent(QPaintEvent * event)
{
const QRect & rect = event->rect();
QPainter painter(this);
painter.eraseRect(rect);
painter.setRenderHint(QPainter::Antialiasing);
drawLines(painter, rect);
}
另一种选择是“负面地”绘制上一次调用
QGraphWidget::paintEvent
的线条。 .如果您存储旧线条的坐标,您可能首先使用背景画笔绘制线条,然后使用前景画笔绘制新线条。查看可能适合您的代码示例 drawLines
方法。请注意,如果您在小部件中绘制其他内容,则负向绘制线条可能会删除一些其他图形,这就是为什么第一种方法,删除所有 GraphWidget
的原因。区,比较好。// save the current brush
QBrush currentBrush = painter.brush();
// draw the old line with the background brush
painter.setBrush(background());
painter.drawLine(oldLine);
// draw the new line with the current brush
painter.setBrush(currentBrush);
painter.drawLine(newLine);
关于qt - 如何在 Qt 中重新绘制新线之前删除/清除以前绘制的线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7625561/