PySide6.QtGui.QPainterStateGuard

class QPainterStateGuard

The QPainterStateGuard is a RAII convenience class for balanced save() and restore() calls. More

Added in version 6.9.

Synopsis

Methods

Note

This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE

Detailed Description

Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

QPainterStateGuard should be used everywhere as a replacement for save() to make sure that the corresponding restore() is called upon finishing of the painting routine to avoid unbalanced calls between those two functions.

Example with save() / restore() :

def paintEvent(self, arg__0):

    painter = QPainter(self)
    painter.setPen(Qt.GlobalColor.red)
    if drawText:
        painter.save()
        painter.setPen(Qt.GlobalColor.blue)
        painter.setFont(QFont("Arial", 30))
        painter.drawText(rect(), Qt.AlignmentFlag.AlignCenter, "Qt")
        painter.restore() # don't forget to restore previous painter state

    painter.drawLine(line)

Example with QPainterStateGuard :

def paintEvent(self, arg__0):

    painter = QPainter(self)
    painter.setPen(Qt.GlobalColor.red)
    if drawText:
        def guard(painter):
        painter.setPen(Qt.GlobalColor.blue)
        painter.setFont(QFont("Arial", 30))
        painter.drawText(rect(), Qt.AlignmentFlag.AlignCenter, "Qt")

    painter.drawLine(line)

See also

QPainter

class InitialState
__init__(painter[, state=QPainterStateGuard.InitialState.Save])
Parameters:

Constructs a QPainterStateGuard and calls save() on painter if state is InitialState::Save (which is the default). When QPainterStateGuard is destroyed, restore() is called as often as save() was called to restore the QPainter ‘s state.

__enter__()
Return type:

QPainterStateGuard

__exit__(arg__1, arg__2, arg__3)
Parameters:
  • arg__1 – object

  • arg__2 – object

  • arg__3 – object

restore()

Calls restore() if the internal save/restore counter is greater than zero.

Note

This function asserts in debug builds if the counter has already reached zero.

save()

Calls save() and increases the internal save/restore counter by one.

swap(other)
Parameters:

otherQPainterStateGuard

Swaps the other with this painter state guard. This operation is very fast and never fails.