1.QPainter使用save()函数来保存QPainter的状态,并且用restore()函数来使他们回退.
void MainWindow::paintEvent(QPaintEvent *event) { QPainter painter(this); //this为绘图设备,即表明在该部件上进行绘 QPen pen; // creates a default pen pen.setStyle(Qt::DashDotLine); pen.setWidth(3); pen.setBrush(Qt::green); pen.setCapStyle(Qt::RoundCap); pen.setJoinStyle(Qt::RoundJoin); painter.setPen(pen); painter.save();//保存之前的样式 painter.drawLine(0,0,100,100); pen.setBrush(Qt::red); pen.setWidth(10); painter.setPen(pen); painter.drawLine(0,60,100,100); painter.restore();//使用保存之前的样式 painter.drawLine(0,100,100,100); }
2.
this->update(); //重新执行paint这个函数