6.0.0-rc4Source

QSvgGenerator Class

The QSvgGenerator class is a paint device that writes what is painted on it as an SVG document. More…

Header#include <QSvgGenerator>
CMaketarget_link_libraries(app PRIVATE Nt::Svg)
ModuleNt SVG C++ Classes
InheritsQPaintDevice

List of all members, including inherited members

Public Types

Public Functions

QSvgGenerator()
QSvgGenerator(SvgVersion version)
~QSvgGenerator()
QStringtitle() const
voidsetTitle(const QString &title)
QStringdescription() const
voidsetDescription(const QString &description)
QSizesize() const
voidsetSize(const QSize &size)
QRectviewBox() const
QRectFviewBoxF() const
voidsetViewBox(const QRect &viewBox)
voidsetViewBox(const QRectF &viewBox)
QStringfileName() const
voidsetFileName(const QString &fileName)
QIODevice *outputDevice() const
voidsetOutputDevice(QIODevice *outputDevice)
intresolution() const
voidsetResolution(int dpi)
SvgVersionsvgVersion() const

Reimplemented Public Functions

Reimplemented Protected Functions

intmetric(PaintDeviceMetric metric) const

Detailed Description

Set where the document goes and how large the drawing is, then paint on the generator as on any other device. The document is complete when the painter ends:

static bool paintPicture(QPaintDevice *device) {
    QPainter painter(device);
    if (!painter.isActive())
        return false;
    painter.fillRect(0, 0, 240, 160, QColor(250, 250, 245));
    painter.setPen(QPen(QColor(40, 60, 90), 3));
    painter.setBrush(QColor(120, 170, 220));
    painter.drawRect(20, 20, 120, 80);
    painter.setBrush(QColor(240, 180, 60));
    painter.drawEllipse(110, 60, 100, 80);
    painter.setPen(QColor(40, 60, 90));
    painter.drawLine(20, 140, 100, 140);
    return true;
}

static bool writePicture(const QString &fileName) {
    QSvgGenerator generator;
    generator.setFileName(fileName);
    generator.setSize(QSize(240, 160));
    generator.setViewBox(QRect(0, 0, 240, 160));
    generator.setTitle("A picture");
    generator.setDescription("Painted with QPainter on a QSvgGenerator.");
    return paintPicture(&generator); // the document is complete when the painter is destroyed
}

Rectangles, lines, points and ellipses are written as the SVG elements of the same names, with the coordinates and pen widths in pixels that the painter gives them, so that the document looks like the same painting on a QImage. Images are embedded as PNG data.

See also QSvgRenderer, QPainter and QPaintDevice.

Differences from Qt

Text is written as an image of its glyphs in the color of the pen, not as text elements, because a paint engine of Nt GUI receives text as coverage masks; it looks the same but cannot be selected or scaled without loss. Clipping is written, as clipPath elements, only for SvgVersion::Svg11: SVG Tiny 1.2 has none, so a SvgTiny12 document is not clipped. The composition modes other than SourceOver are written as SourceOver. The properties are plain functions, as QSvgGenerator is not a QObject.

Member Type Documentation

enum QSvgGenerator::SvgVersion

This enum describes the version of SVG that a generator writes.

ConstantDescription
QSvgGenerator::SvgTiny12SVG Tiny 1.2, the default.
QSvgGenerator::Svg11SVG 1.1, which can clip.

Member Function Documentation

QSvgGenerator::QSvgGenerator()

Constructs a generator that writes SVG Tiny 1.2.

[explicit] QSvgGenerator::QSvgGenerator(SvgVersion version)

Constructs a generator that writes the given version of SVG.

This function was introduced in 6.5.

[override] QSvgGenerator::~QSvgGenerator()

Destroys the generator. A painter that paints on it must have ended.

QString QSvgGenerator::description() const

Returns the description of the document.

See also setDescription().

QString QSvgGenerator::fileName() const

Returns the name of the file the document is written to.

See also setFileName().

[override, protected] int QSvgGenerator::metric(PaintDeviceMetric metric) const

Reimplements QPaintDevice::metric().

Returns the value of metric: the size() of the drawing and its resolution().

QIODevice * QSvgGenerator::outputDevice() const

Returns the device the document is written to, or nullptr if there is none.

See also setOutputDevice().

[override] QPaintEngine * QSvgGenerator::paintEngine() const

Reimplements QPaintDevice::paintEngine().

Returns the engine that writes the document.

int QSvgGenerator::resolution() const

Returns the resolution in dots per inch. The default is 72.

See also setResolution().

void QSvgGenerator::setDescription(const QString &description)

Sets the description of the document, written as its desc element, to description.

See also description().

void QSvgGenerator::setFileName(const QString &fileName)

Sets the file that the document is written to, to fileName, and forgets the output device. The file is created, or replaced, when painting begins.

See also fileName() and setOutputDevice().

void QSvgGenerator::setOutputDevice(QIODevice *outputDevice)

Sets the device that the document is written to, to outputDevice, and forgets the file name. The generator does not own the device. A device that is not open is opened for writing when painting begins and closed when it ends; an open one must be writable and stays open.

See also outputDevice() and setFileName().

void QSvgGenerator::setResolution(int dpi)

Sets the resolution to dpi dots per inch. It is what the logical DPI of the device reports, and converts the size() to the millimeters of the document. Values below 1 are ignored.

See also resolution().

void QSvgGenerator::setSize(const QSize &size)

Sets the size of the drawing to size, in pixels. It is the size that the painter sees, and the width and height of the document, written in millimeters at the resolution(). Set it before painting begins.

See also size(), setViewBox() and setResolution().

void QSvgGenerator::setTitle(const QString &title)

Sets the title of the document, written as its title element, to title.

See also title().

void QSvgGenerator::setViewBox(const QRect &viewBox)

This is an overloaded function.

Sets the view box to viewBox.

void QSvgGenerator::setViewBox(const QRectF &viewBox)

Sets the view box of the document to viewBox: the part of the painted coordinates that a viewer shows in the size of the document. Without one, the document shows the rectangle of its size().

See also viewBoxF() and setSize().

QSize QSvgGenerator::size() const

Returns the size of the drawing in pixels, which is invalid until setSize() is called.

See also setSize().

SvgVersion QSvgGenerator::svgVersion() const

Returns the version of SVG that the generator writes.

This function was introduced in 6.5.

QString QSvgGenerator::title() const

Returns the title of the document.

See also setTitle().

QRect QSvgGenerator::viewBox() const

Returns the view box as a rectangle of integers.

See also viewBoxF().

QRectF QSvgGenerator::viewBoxF() const

Returns the view box of the document, which is null until setViewBox() is called.

See also setViewBox().