PySide6.QtGui.QPainterStateGuard¶
- class QPainterStateGuard¶
The
QPainterStateGuard
is a RAII convenience class for balancedsave()
andrestore()
calls. More…Added in version 6.9.
Synopsis¶
Methods¶
def
__init__()
def
__enter__()
def
__exit__()
def
restore()
def
save()
def
swap()
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 forsave()
to make sure that the correspondingrestore()
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
- class InitialState¶
- __init__(painter[, state=QPainterStateGuard.InitialState.Save])¶
- Parameters:
painter –
QPainter
state –
InitialState
Constructs a
QPainterStateGuard
and callssave()
onpainter
ifstate
isInitialState::Save
(which is the default). WhenQPainterStateGuard
is destroyed,restore()
is called as often assave()
was called to restore theQPainter
‘s state.- __enter__()¶
- Return type:
- __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:
other –
QPainterStateGuard
Swaps the
other
with this painter state guard. This operation is very fast and never fails.