Revision control

Copy as Markdown

// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
/*!
\page svgrendering.html
\title Rendering SVG Files
\brief Rendering SVG files with the Qt SVG module
SVG drawings can be rendered onto any \l QPaintDevice subclass, such as
\l QWidget or \l QImage. The easiest way to render SVG files is to use
\l QSvgWidget or \l QSvgRenderer.
\section1 Using QSvgWidget
\l QSvgWidget provides a convenient widget for displaying SVG files.
\code
#include <QApplication>
#include <QSvgWidget>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QSvgWidget svgWidget(QStringLiteral(":/images/example.svg"));
svgWidget.setGeometry(100, 100, 400, 400);
svgWidget.show();
return app.exec();
}
\endcode
You can also load an SVG file after construction:
\code
#include <QApplication>
#include <QSvgWidget>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QSvgWidget *svgWidget = new QSvgWidget;
svgWidget->load(QStringLiteral(":/images/example.svg"));
svgWidget->show();
return app.exec();
}
\endcode
\section1 Using QSvgRenderer
Use \l QSvgRenderer to render SVG content onto any \l QPaintDevice.
\code
#include <QApplication>
#include <QSvgRenderer>
#include <QImage>
#include <QPainter>
#include <QLabel>
#include <QPixmap>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QSvgRenderer renderer(QStringLiteral(":/images/example.svg"));
if (renderer.isValid()) {
QImage image(400, 400, QImage::Format_ARGB32_Premultiplied);
image.fill(Qt::transparent);
QPainter painter(&image);
renderer.render(&painter);
QLabel label;
label.setPixmap(QPixmap::fromImage(image));
label.resize(400, 400);
label.show();
return app.exec();
}
return 1;
}
\endcode
You can also use \l QSvgRenderer to render SVG directly in a custom widget's
paint event:
\code
// In your widget's header file:
#include <QSvgRenderer>
class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget(QWidget *parent = nullptr)
: QWidget(parent), renderer(QStringLiteral(":/images/example.svg")) {}
protected:
void paintEvent(QPaintEvent *) override {
QPainter painter(this);
renderer.render(&painter);
}
private:
QSvgRenderer renderer;
};
\endcode
These approaches allow you to render SVG files on all paint devices
supported by Qt, including \l QWidget and \l QImage.
\section2 Rendering options
\l QSvgRenderer provides rendering \l {QSvgRenderer::}{options} via the
\l {QtSvg::Option} enum. These options let you control how SVG files are
parsed and rendered.
\section2 Rendering animated SVG files
The Qt SVG module supports rendering animated SVG files. Refer to the
\l QSvgRenderer class documentation for details on how to work with animated
SVGs.
\sa {Vector Image Formats in Qt}, {Extended Features}
*/