painter = QPainter() painter.save() # save the current state # ... draw some shapes ... painter.restore() # restore the previous state
painter = QPainter() painter.save() # save the current state painter.setPen(QPen(Qt.blue, 3)) # ... draw some shapes ... painter.restore() # restore the previous stateThis code saves the current state of the painter, sets the pen color to blue with a width of 3, draws some shapes, and then restores the previous state of the painter. In both examples, we are using the PySide2.QtGui package library in Python to utilize the QPainter class with the save() and restore() functions.