Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
4#include "qsvgwidget.h"
5
6#include <qsvgrenderer.h>
7
8#include "qstyleoption.h"
9#include "qpainter.h"
10#include <QtWidgets/private/qwidget_p.h>
11
13
45{
46 Q_DECLARE_PUBLIC(QSvgWidget)
47public:
49};
50
51// Event listener for ShowEvent/HideEvent on QSvgWidget (which can't just
52// reimplement event() or showEvent()/hideEvent() or eventFilter() in case
53// a subclass doesn't call the base class in those methods)
54// ### Qt 7: remove the event filter; move this logic into showEvent/hideEvent; add event() override
56{
57public:
58 using QObject::QObject;
59
60protected:
62 {
63 if (event->type() == QEvent::Show)
64 renderer()->setAnimationEnabled(true);
65 else if (event->type() == QEvent::Hide)
66 renderer()->setAnimationEnabled(false);
68 }
69
70private:
71 QSvgRenderer *renderer()
72 {
73 return static_cast<QSvgWidgetPrivate *>(QObjectPrivate::get(parent()))->renderer;
74 }
75};
76
81 : QWidget(*new QSvgWidgetPrivate, parent, {})
82{
83 d_func()->renderer = new QSvgRenderer(this);
84 QObject::connect(d_func()->renderer, SIGNAL(repaintNeeded()),
85 this, SLOT(update()));
87}
88
94 : QSvgWidget(parent)
95{
96 d_func()->renderer->load(file);
97}
98
106
111{
112 Q_D(const QSvgWidget);
113 return d->renderer;
114}
115
116
121{
122 Q_D(const QSvgWidget);
123 if (d->renderer->isValid())
124 return d->renderer->defaultSize();
125 else
126 return QSize(128, 64);
127}
128
136QtSvg::Options QSvgWidget::options() const
137{
138 Q_D(const QSvgWidget);
139 return d->renderer->options();
140}
141
156void QSvgWidget::setOptions(QtSvg::Options options)
157{
158 Q_D(QSvgWidget);
159 d->renderer->setOptions(options);
160}
161
166{
167 Q_D(QSvgWidget);
169 opt.initFrom(this);
170 QPainter p(this);
172 d->renderer->render(&p);
173}
174
179{
180 Q_D(const QSvgWidget);
181 d->renderer->load(file);
182 if (!isVisible())
183 d->renderer->setAnimationEnabled(false);
184}
185
190{
191 Q_D(const QSvgWidget);
192 d->renderer->load(contents);
193 if (!isVisible())
194 d->renderer->setAnimationEnabled(false);
195}
196
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore
Definition qcoreevent.h:45
static QObjectPrivate * get(QObject *o)
Definition qobject_p.h:150
\inmodule QtCore
Definition qobject.h:103
Q_INVOKABLE QObject(QObject *parent=nullptr)
Constructs an object with parent object parent.
Definition qobject.cpp:936
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
virtual bool eventFilter(QObject *watched, QEvent *event)
Filters events if this object has been installed as an event filter for the watched object.
Definition qobject.cpp:1555
The QPaintEvent class contains event parameters for paint events.
Definition qevent.h:486
The QPainter class performs low-level painting on widgets and other paint devices.
Definition qpainter.h:46
\inmodule QtCore
Definition qsize.h:25
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
The QStyleOption class stores the parameters used by QStyle functions.
void initFrom(const QWidget *w)
@ PE_Widget
Definition qstyle.h:148
virtual void drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w=nullptr) const =0
Draws the given primitive element with the provided painter using the style options specified by opti...
\inmodule QtSvg
bool load(const QString &filename)
Loads the SVG file specified by filename, returning true if the content was successfully parsed; othe...
void setAnimationEnabled(bool enable)
bool eventFilter(QObject *obj, QEvent *event) override
Filters events if this object has been installed as an event filter for the watched object.
QSvgRenderer * renderer
\inmodule QtSvgWidgets
Definition qsvgwidget.h:19
QSvgWidget(QWidget *parent=nullptr)
Constructs a new SVG display widget with the given parent.
QSize sizeHint() const override
\reimp
void load(const QString &file)
Loads the contents of the specified SVG file and updates the widget.
QSvgRenderer * renderer() const
Returns the renderer used to display the contents of the widget.
void setOptions(QtSvg::Options options)
void paintEvent(QPaintEvent *event) override
\reimp
QtSvg::Options options() const
~QSvgWidget()
Destroys the widget.
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
QStyle * style() const
Definition qwidget.cpp:2600
bool isVisible() const
Definition qwidget.h:874
QStyleOptionButton opt
Combined button and popup list for selecting options.
#define SLOT(a)
Definition qobjectdefs.h:52
#define SIGNAL(a)
Definition qobjectdefs.h:53
struct _cl_event * event
GLhandleARB obj
[2]
GLfloat GLfloat p
[1]
QFile file
[0]
monitoredObj installEventFilter(filterObj)
[13]
QSvgRenderer * renderer
[0]