En esta página

QPainterStateGuard Class

QPainterStateGuard es una clase de conveniencia de RAII para llamadas balanceadas a QPainter::save() y QPainter::restore(). Más...

Cabecera: #include <QPainterStateGuard>
CMake: find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
qmake: QT += gui
Desde: Qt 6.9

Nota: Todas las funciones de esta clase son reentrantes.

Funciones Públicas

QPainterStateGuard(QPainter *painter, QPainterStateGuard::InitialState state = InitialState::Save)
QPainterStateGuard(QPainterStateGuard &&other)
~QPainterStateGuard()
void restore()
void save()
void swap(QPainterStateGuard &other)
QPainterStateGuard &operator=(QPainterStateGuard &&other)

Descripción Detallada

QPainterStateGuard debe usarse en todas partes como reemplazo de QPainter::save() para asegurarse de que la correspondiente QPainter::restore() es llamada al finalizar la rutina de pintado para evitar llamadas desequilibradas entre esas dos funciones.

Ejemplo con 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);
}

Ejemplo con 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);
}

Ver también QPainter.

Documentación de Funciones Miembro

[explicit] QPainterStateGuard::QPainterStateGuard(QPainter *painter, QPainterStateGuard::InitialState state = InitialState::Save)

Construye un QPainterStateGuard y llama a save() en painter si state es InitialState::Save (que es el valor por defecto). Cuando se destruye QPainterStateGuard, se llama a restore() tantas veces como se llamó a save() para restaurar el estado de QPainter.

[noexcept] QPainterStateGuard::QPainterStateGuard(QPainterStateGuard &&other)

Mueve-construye una guardia de estado pintor desde other.

[noexcept] QPainterStateGuard::~QPainterStateGuard()

Destruye la instancia QPainterStateGuard y llama a restore() tantas veces como save() haya sido llamada para restaurar el estado de QPainter.

void QPainterStateGuard::restore()

Llama a QPainter::restore() si el contador interno de guardar/restaurar es mayor que cero.

Nota: Esta función se activa en construcciones de depuración si el contador ya ha llegado a cero.

void QPainterStateGuard::save()

Llama a QPainter::save() y aumenta en uno el contador interno de guardar/restaurar.

[noexcept] void QPainterStateGuard::swap(QPainterStateGuard &other)

Cambia el other con este pintor guardia de estado. Esta operación es muy rápida y nunca falla.

[noexcept] QPainterStateGuard &QPainterStateGuard::operator=(QPainterStateGuard &&other)

Mover-asigna other a esta guardia de estado de pintor.

© 2026 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.