QPainter painter(this); painter.drawRect(20, 20, 100, 100); QTransform transform; transform.translate(50, 50); transform.rotate(45); painter.save(); painter.setTransform(transform); painter.drawRect(0, 0, 100, 100); painter.restore(); painter.drawRect(150, 150, 100, 100);
void MyWidget::paintEvent(QPaintEvent* event) { QPainter painter(this); painter.save(); painter.setBrush(QBrush(Qt::white)); painter.drawRect(0, 0, width(), height()); painter.restore(); painter.setPen(Qt::red); painter.drawLine(0, 0, width(), height()); painter.drawLine(0, height(), width(), 0); }This example shows how to use restore to set a background color for a widget when it is painted. The current painter state is saved, a white fill is set, and a rectangle is drawn. The painter state is restored, and then two diagonal lines are drawn with a red pen. Package Library: QtGui (part of Qt Toolkit)