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
qsvgwidget.cpp
Go to the documentation of this file.
1
// Copyright (C) 2020 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
// Qt-Security score:significant reason:default
4
5
6
#
include
"qsvgwidget.h"
7
8
#
include
<
qsvgrenderer
.
h
>
9
10
#
include
"qstyleoption.h"
11
#
include
"qpainter.h"
12
#
include
<
QtWidgets
/
private
/
qwidget_p
.
h
>
13
14
QT_BEGIN_NAMESPACE
15
16
/*!
17
\class QSvgWidget
18
\inmodule QtSvgWidgets
19
\ingroup painting
20
21
\brief The QSvgWidget class provides a widget that is used to display the contents of
22
Scalable Vector Graphics (SVG) files.
23
\since 4.1
24
25
This class enables developers to display SVG drawings alongside standard widgets, and
26
is used in much the same way as QLabel is used for displaying text and bitmap images.
27
28
Since QSvgWidget is a subclass of QWidget, SVG drawings are rendered using the properties
29
of the display. More control can be exercised over the rendering process with the
30
QSvgRenderer class, as this can be used to paint onto other paint devices, such as QImage
31
and QGLWidget. The renderer used by the widget can be obtained with the renderer()
32
function.
33
34
Each QSvgWidget can be constructed with the file name of a SVG file, or they can be
35
constructed without a specific file to render and one can be supplied later. The load()
36
functions provide two different ways to load an SVG file: they accept either the file name
37
of an SVG file or a QByteArray containing the serialized XML representation of an SVG file.
38
39
By default, the widget provides a size hint to reflect the size of the drawing that it
40
displays. If no data has been loaded, the widget provides the default QWidget size hint.
41
Subclass this class and reimplement sizeHint() if you need to customize this behavior.
42
43
\sa QSvgRenderer, {Qt SVG C++ Classes}, QPicture
44
*/
45
46
class
QSvgWidgetPrivate
:
public
QWidgetPrivate
47
{
48
Q_DECLARE_PUBLIC(QSvgWidget)
49
public
:
50
QSvgRenderer
*
renderer
;
51
};
52
53
// Event listener for ShowEvent/HideEvent on QSvgWidget (which can't just
54
// reimplement event() or showEvent()/hideEvent() or eventFilter() in case
55
// a subclass doesn't call the base class in those methods)
56
// ### Qt 7: remove the event filter; move this logic into showEvent/hideEvent; add event() override
57
class
QSvgWidgetListener
:
public
QObject
58
{
59
public
:
60
using
QObject
::
QObject
;
61
62
protected
:
63
bool
eventFilter
(QObject *obj, QEvent *event)
override
64
{
65
if
(event->type() == QEvent::Show)
66
renderer()->setAnimationEnabled(
true
);
67
else
if
(event->type() == QEvent::Hide)
68
renderer()->setAnimationEnabled(
false
);
69
return
QObject::eventFilter(obj, event);
70
}
71
72
private
:
73
QSvgRenderer *renderer()
74
{
75
return
static_cast
<QSvgWidgetPrivate *>(QObjectPrivate::get(parent()))->renderer;
76
}
77
};
78
79
/*!
80
Constructs a new SVG display widget with the given \a parent.
81
*/
82
QSvgWidget::QSvgWidget(QWidget *parent)
83
: QWidget(*
new
QSvgWidgetPrivate, parent, {})
84
{
85
d_func()->renderer =
new
QSvgRenderer(
this
);
86
QObject::connect(d_func()->renderer, SIGNAL(repaintNeeded()),
87
this
, SLOT(update()));
88
installEventFilter(
new
QSvgWidgetListener(
this
));
89
}
90
91
/*!
92
Constructs a new SVG display widget with the given \a parent and loads the contents
93
of the specified \a file.
94
*/
95
QSvgWidget::QSvgWidget(
const
QString &file, QWidget *parent)
96
: QSvgWidget(parent)
97
{
98
d_func()->renderer->load(file);
99
}
100
101
/*!
102
Destroys the widget.
103
*/
104
QSvgWidget::~QSvgWidget()
105
{
106
107
}
108
109
/*!
110
Returns the renderer used to display the contents of the widget.
111
*/
112
QSvgRenderer * QSvgWidget::renderer()
const
113
{
114
Q_D(
const
QSvgWidget);
115
return
d->renderer;
116
}
117
118
119
/*!
120
\reimp
121
*/
122
QSize QSvgWidget::sizeHint()
const
123
{
124
Q_D(
const
QSvgWidget);
125
if
(d->renderer->isValid())
126
return
d->renderer->defaultSize();
127
else
128
return
QSize(128, 64);
129
}
130
131
/*!
132
\since 6.7
133
134
Returns the options of the widget's renderer.
135
136
\sa setOptions
137
*/
138
QtSvg::Options QSvgWidget::options()
const
139
{
140
Q_D(
const
QSvgWidget);
141
return
d->renderer->options();
142
}
143
144
/*!
145
\since 6.7
146
147
Sets the widget's renderer options to \a options.
148
149
This property holds a set of QtSvg::Option flags that can be used
150
to enable or disable various features of the parsing and rendering
151
of SVG files. It must be set before calling the load function to
152
have any effect.
153
154
By default, no flags are set.
155
156
\sa options
157
*/
158
void
QSvgWidget::setOptions(QtSvg::Options options)
159
{
160
Q_D(QSvgWidget);
161
d->renderer->setOptions(options);
162
}
163
164
/*!
165
\reimp
166
*/
167
void
QSvgWidget::paintEvent(QPaintEvent *)
168
{
169
Q_D(QSvgWidget);
170
QStyleOption opt;
171
opt.initFrom(
this
);
172
QPainter p(
this
);
173
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p,
this
);
174
d->renderer->render(&p);
175
}
176
177
/*!
178
Loads the contents of the specified SVG \a file and updates the widget.
179
*/
180
void
QSvgWidget::load(
const
QString &file)
181
{
182
Q_D(
const
QSvgWidget);
183
d->renderer->load(file);
184
if
(!isVisible())
185
d->renderer->setAnimationEnabled(
false
);
186
}
187
188
/*!
189
Loads the specified SVG format \a contents and updates the widget.
190
*/
191
void
QSvgWidget::load(
const
QByteArray &contents)
192
{
193
Q_D(
const
QSvgWidget);
194
d->renderer->load(contents);
195
if
(!isVisible())
196
d->renderer->setAnimationEnabled(
false
);
197
}
198
199
QT_END_NAMESPACE
QSvgWidgetListener
Definition
qsvgwidget.cpp:58
QSvgWidgetListener::eventFilter
bool eventFilter(QObject *obj, QEvent *event) override
Filters events if this object has been installed as an event filter for the watched object.
Definition
qsvgwidget.cpp:63
QSvgWidgetPrivate
Definition
qsvgwidget.cpp:47
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qrandomaccessasyncfile_darwin.mm:17
qtsvg
src
svgwidgets
qsvgwidget.cpp
Generated on
for Qt by
1.16.1