Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
src_gui_painting_qpainterstateguard.cpp
Go to the documentation of this file.
1// Copyright (C) 2024 Christian Ehrlicher <ch.ehrlicher@gmx.de>
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3#include <QPaintEvent>
4#include <QPainter>
5#include <QPainterStateGuard>
6#include <QWidget>
7
8
10struct MyWidget : public QWidget
11{
12 void paintEvent(QPaintEvent *) override;
13 bool drawText = true;
15};
16struct MyGuardWidget : public QWidget
17{
18 void paintEvent(QPaintEvent *) override;
19 bool drawText = true;
21};
22
23//! [0]
24void MyWidget::paintEvent(QPaintEvent *)
25{
26 QPainter painter(this);
27 painter.setPen(Qt::red);
28 if (drawText) {
29 painter.save();
30 painter.setPen(Qt::blue);
31 painter.setFont(QFont("Arial", 30));
32 painter.drawText(rect(), Qt::AlignCenter, "Qt");
33 painter.restore(); // don't forget to restore previous painter state
34 }
35 painter.drawLine(line);
36}
37//! [0]
38
39//! [1]
40void MyGuardWidget::paintEvent(QPaintEvent *)
41{
42 QPainter painter(this);
43 painter.setPen(Qt::red);
44 if (drawText) {
45 QPainterStateGuard guard(&painter);
46 painter.setPen(Qt::blue);
47 painter.setFont(QFont("Arial", 30));
48 painter.drawText(rect(), Qt::AlignCenter, "Qt");
49 }
50 painter.drawLine(line);
51}
52//! [1]
53
54} // src_gui_painting_qpainterstateguard