このページでは

QPainterStateGuard Class

QPainterStateGuard は、QPainter::save() およびQPainter::restore() の呼び出しをバランスよく行うための RAII 利便性クラスです。詳細...

ヘッダー: #include <QPainterStateGuard>
CMake: find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
qmake: QT += gui
以下のように: Qt 6.9

注:このクラスのすべての関数は再入可能である。

パブリック関数

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

詳細な説明

QPainterStateGuard は、QPainter::save() の代わりとしてあらゆる場所で使用し、ペイントルーチンの終了時に、対応するQPainter::restore() が確実に呼び出されるようにすることで、これら 2 つの関数間の呼び出しの不均衡を回避する必要があります。

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

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

QPainterも参照してください 。

メンバ関数のドキュメント

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

QPainterStateGuard を生成し、state がInitialState::Save (デフォルト値)の場合、painter に対してsave() を呼び出します。QPainterStateGuard が破棄されると、restore() は、save() が呼び出された回数と同じ回数だけ呼び出され、QPainter の状態が復元されます。

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

other からペインター状態ガードをムーブコンストラクトで生成します。

[noexcept] QPainterStateGuard::~QPainterStateGuard()

QPainterStateGuard インスタンスを破棄し、save()が呼び出された回数と同じ回数だけrestore()を呼び出して、QPainter の状態を復元します。

void QPainterStateGuard::restore()

内部の保存/復元カウンタがゼロより大きい場合、QPainter::restore() を呼び出します。

注: デバッグビルドでは、カウンタがすでにゼロに達している場合、この関数は アサートします。

void QPainterStateGuard::save()

QPainter::save() を呼び出し、内部の保存/復元カウンタを1増やします。

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

other を、このペインター状態ガードと入れ替えます。この操作は非常に高速で、失敗することはありません。

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

other をこのペインターのステートガードに割り当てます。

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