QPainterStateGuard Class
The QPainterStateGuard is a RAII convenience class for balanced QPainter::save() and QPainter::restore() calls. More...
Header: | #include <QPainterStateGuard> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS Gui) target_link_libraries(mytarget PRIVATE Qt6::Gui) |
qmake: | QT += gui |
Since: | Qt 6.9 |
- List of all members, including inherited members
- QPainterStateGuard is part of Painting Classes.
Note: All functions in this class are reentrant.
Public Functions
QPainterStateGuard(QPainter *painter, QPainterStateGuard::InitialState state = InitialState::Save) | |
~QPainterStateGuard() | |
void | restore() |
void | save() |
Detailed Description
QPainterStateGuard should be used everywhere as a replacement for QPainter::save() to make sure that the corresponding QPainter::restore() is called upon finishing of the painting routine to avoid unbalanced calls between those two functions.
Example with QPainter::save()/QPainter::restore():
void MyWidget::paintEvent(QPaintEvent *) { QPainter painter(this); painter.setPen(Qt::red); if (drawText) { painter.save(); painter.setPen(Qt::blue); painter.setFont(QFont("Arial", 30)); painter.drawText(rect(), Qt::AlignCenter, "Qt"); painter.restore(); // don't forget to restore previous painter state } painter.drawLine(line); }
Example with QPainterStateGuard:
void MyGuardWidget::paintEvent(QPaintEvent *) { QPainter painter(this); painter.setPen(Qt::red); if (drawText) { QPainterStateGuard guard(&painter) painter.setPen(Qt::blue); painter.setFont(QFont("Arial", 30)); painter.drawText(rect(), Qt::AlignCenter, "Qt"); } painter.drawLine(line); }
See also QPainter.
Member Function Documentation
[explicit]
QPainterStateGuard::QPainterStateGuard(QPainter *painter, QPainterStateGuard::InitialState state = InitialState::Save)
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.
[noexcept]
QPainterStateGuard::~QPainterStateGuard()
Destroys the QPainterStateGuard instance and calls restore() as often as save() was called to restore the QPainter's state.
void QPainterStateGuard::restore()
Calls QPainter::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.
void QPainterStateGuard::save()
Calls QPainter::save() and increases the internal save/restore counter by one.
© 2024 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.