Nesta página

QPainterStateGuard Class

O QPainterStateGuard é uma classe de conveniência RAII para chamadas equilibradas de ` QPainter::save()` e ` QPainter::restore()`. Mais...

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

Observação: Todas as funções desta classe são reentrantes.

Funções 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)

Descrição detalhada

O QPainterStateGuard deve ser usado em todos os casos como substituto de ` QPainter::save()` para garantir que a função correspondente ` QPainter::restore()` seja chamada ao término da rotina de pintura, a fim de evitar desequilíbrios nas chamadas entre essas duas funções.

Exemplo com 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);
}

Exemplo com 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);
}

Veja também QPainter.

Documentação das funções de membro

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

Cria um QPainterStateGuard e chama ` save()` em ` painter ` se ` state ` for ` InitialState::Save ` (que é o padrão). Quando o QPainterStateGuard é destruído, ` restore()` é chamado tantas vezes quantas foram as chamadas a ` save()` para restaurar o estado do ` QPainter`.

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

Cria um guard de estado do pintor a partir de ` other`.

[noexcept] QPainterStateGuard::~QPainterStateGuard()

Destrói a instância de ` QPainterStateGuard ` e chama ` restore()` tantas vezes quanto ` save()` foi chamado para restaurar o estado de ` QPainter`.

void QPainterStateGuard::restore()

Chama QPainter::restore() se o contador interno de salvamento/restauração for maior que zero.

Observação: essa função gera uma exceção em compilações de depuração se o contador já tiver chegado a zero.

void QPainterStateGuard::save()

Chama QPainter::save() e aumenta em um o contador interno de salvamento/restauração.

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

Troca o ` other ` por este `painter state guard`. Essa operação é muito rápida e nunca falha.

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

A instrução `move` atribui ` other ` a essa variável de guarda do estado do 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.