En esta página

QPainterStateGuard Class

QPainterStateGuard es una clase de conveniencia RAII que permite realizar llamadas equilibradas a ` QPainter::save()` y ` QPainter::restore()`. Más información...

Encabezado: #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 utilizarse en todas partes como sustituto de QPainter::save() para garantizar que se llame a la función correspondiente QPainter::restore() al finalizar la rutina de dibujo, con el fin de evitar un desequilibrio en las llamadas entre ambas 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);
}

Véase también QPainter.

Documentación de las funciones miembro

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

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

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

Crea un guard de estado de pintor a partir de other.

[noexcept] QPainterStateGuard::~QPainterStateGuard()

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

void QPainterStateGuard::restore()

Llama a QPainter::restore() si el contador interno de guardado/restauración es mayor que cero.

Nota: Esta función genera un error en las compilaciones 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 guardado/restauración.

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

Cambia el « other » por este guardia estatal de la pintura. Esta operación es muy rápida y nunca falla.

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

Asigna mediante «move» la variable ` other ` a esta variable de estado del 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.