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
svgrendering.qdoc
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5 \page svgrendering.html
6 \title Rendering SVG Files
7 \brief Rendering SVG files with the Qt SVG module
8
9 SVG drawings can be rendered onto any \l QPaintDevice subclass, such as
10 \l QWidget or \l QImage. The easiest way to render SVG files is to use
11 \l QSvgWidget or \l QSvgRenderer.
12
13 \section1 Using QSvgWidget
14
15 \l QSvgWidget provides a convenient widget for displaying SVG files.
16
17 \code
18 #include <QApplication>
19 #include <QSvgWidget>
20
21 int main(int argc, char *argv[])
22 {
23 QApplication app(argc, argv);
24 QSvgWidget svgWidget(QStringLiteral(":/images/example.svg"));
25 svgWidget.setGeometry(100, 100, 400, 400);
26 svgWidget.show();
27 return app.exec();
28 }
29 \endcode
30
31 You can also load an SVG file after construction:
32
33 \code
34 #include <QApplication>
35 #include <QSvgWidget>
36
37 int main(int argc, char *argv[])
38 {
39 QApplication app(argc, argv);
40 QSvgWidget *svgWidget = new QSvgWidget;
41 svgWidget->load(QStringLiteral(":/images/example.svg"));
42 svgWidget->show();
43 return app.exec();
44 }
45 \endcode
46
47 \section1 Using QSvgRenderer
48
49 Use \l QSvgRenderer to render SVG content onto any \l QPaintDevice.
50
51 \code
52 #include <QApplication>
53 #include <QSvgRenderer>
54 #include <QImage>
55 #include <QPainter>
56 #include <QLabel>
57 #include <QPixmap>
58
59 int main(int argc, char *argv[])
60 {
61 QApplication app(argc, argv);
62 QSvgRenderer renderer(QStringLiteral(":/images/example.svg"));
63 if (renderer.isValid()) {
64 QImage image(400, 400, QImage::Format_ARGB32_Premultiplied);
65 image.fill(Qt::transparent);
66 QPainter painter(&image);
67 renderer.render(&painter);
68
69 QLabel label;
70 label.setPixmap(QPixmap::fromImage(image));
71 label.resize(400, 400);
72 label.show();
73 return app.exec();
74 }
75 return 1;
76 }
77 \endcode
78
79 You can also use \l QSvgRenderer to render SVG directly in a custom widget's
80 paint event:
81
82 \code
83 // In your widget's header file:
84 #include <QSvgRenderer>
85
86 class MyWidget : public QWidget
87 {
88 Q_OBJECT
89 public:
90 MyWidget(QWidget *parent = nullptr)
91 : QWidget(parent), renderer(QStringLiteral(":/images/example.svg")) {}
92
93 protected:
94 void paintEvent(QPaintEvent *) override {
95 QPainter painter(this);
96 renderer.render(&painter);
97 }
98
99 private:
100 QSvgRenderer renderer;
101 };
102 \endcode
103
104 These approaches allow you to render SVG files on all paint devices
105 supported by Qt, including \l QWidget and \l QImage.
106
107
108 \section2 Rendering options
109
110 \l QSvgRenderer provides rendering \l {QSvgRenderer::}{options} via the
111 \l {QtSvg::Option} enum. These options let you control how SVG files are
112 parsed and rendered.
113
114 \section2 Rendering animated SVG files
115
116 The Qt SVG module supports rendering animated SVG files. Refer to the
117 \l QSvgRenderer class documentation for details on how to work with animated
118 SVGs.
119
120 \sa {Vector Image Formats in Qt}, {Extended Features}
121*/