QSvgRenderer Class
The QSvgRenderer class draws the contents of SVG files with a QPainter. More…
| Header | #include <QSvgRenderer> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Svg) |
| Module | Nt SVG C++ Classes |
| Inherits | QObject |
List of all members, including inherited members
Public Functions
| QSvgRenderer(QObject *parent = nullptr) | |
| QSvgRenderer(const QString &filename, QObject *parent = nullptr) | |
| QSvgRenderer(const QByteArray &contents, QObject *parent = nullptr) | |
| ~QSvgRenderer() | |
| bool | isValid() const |
| QSize | defaultSize() const |
| QRect | viewBox() const |
| QRectF | viewBoxF() const |
| void | setViewBox(const QRect &viewbox) |
| void | setViewBox(const QRectF &viewbox) |
| Qt::AspectRatioMode | aspectRatioMode() const |
| void | setAspectRatioMode(Qt::AspectRatioMode mode) |
| QtSvg::Options | options() const |
| void | setOptions(QtSvg::Options flags) |
| bool | animated() const |
| int | framesPerSecond() const |
| void | setFramesPerSecond(int num) |
| int | currentFrame() const |
| void | setCurrentFrame(int frame) |
| int | animationDuration() const |
| bool | isAnimationEnabled() const |
| void | setAnimationEnabled(bool enable) |
| QRectF | boundsOnElement(const QString &id) const |
| bool | elementExists(const QString &id) const |
Public Slots
| bool | load(const QString &filename) |
| bool | load(const QByteArray &contents) |
| void | render(QPainter *painter) |
| void | render(QPainter *painter, const QRectF &bounds) |
| void | render(QPainter *painter, const QString &elementId, const QRectF &bounds = QRectF()) |
Signals
| void | repaintNeeded() |
Detailed Description
Load a document from a file, a resource or a byte array, and draw it on any paint device:
static QImage renderToImage(const QByteArray &svg, const QSize &size) {
QSvgRenderer renderer(svg);
QImage image(size, QImage::Format_ARGB32_Premultiplied);
image.fill(Qt::transparent);
if (renderer.isValid()) {
renderer.setAspectRatioMode(Qt::KeepAspectRatio);
QPainter painter(&image);
renderer.render(&painter);
}
return image;
}
The document is drawn into a rectangle: all of the device with render(QPainter *), or the bounds given. The view box of the document is stretched to fill the rectangle unless aspectRatioMode() says otherwise. defaultSize() is the size the document asks for. One element can be drawn alone, by its id, and boundsOnElement() tells where it is; a sheet of icons in one file can be used that way.
Shapes are filled and stroked with antialiasing, whatever the painter can do itself: the renderer draws the document at the resolution of the device and gives the result to the painter as an image.
What Is Drawn
The structure elements svg, g, defs, symbol, use, a and switch; the shapes path, rect, circle, ellipse, line, polyline and polygon; text with tspan; linearGradient and radialGradient; and clipPath. Fills and strokes take colors (names, #rgb, rgb(), currentColor) and gradients, with opacity, both fill rules, line caps and joins, miter limits and dashes. Properties come from attributes, the style attribute and style elements, whose rules may select by element name, class and id. Transformations, group opacity and lengths with units are supported.
See also QSvgWidget, QSvgGenerator and QPainter.
Differences from Qt
Documents are static: no SMIL or CSS animation, so animated() is false and repaintNeeded() is never emitted. Not drawn: image, mask, pattern, marker, filters, foreignObject, textPath and fonts in the document. Text is drawn upright with the fonts of the system: it follows the position and scale of its transformation but not rotation or shear, takes one color and has no stroke. preserveAspectRatio of the document is not read; use setAspectRatioMode(). CSS selectors with combinators or pseudo-classes are skipped. There is no load() from a QXmlStreamReader, no transformForElement() (there is no QTransform) and no setDefaultOptions(). Of the options, only QtSvg::AssumeTrustedSource has an effect.
Member Function Documentation
QSvgRenderer::QSvgRenderer(QObject *parent = nullptr)
Constructs a renderer without a document, with the given parent.
See also load().
QSvgRenderer::QSvgRenderer(const QString &filename, QObject *parent = nullptr)
Constructs a renderer with the given parent and loads the file filename, as load() does.
QSvgRenderer::QSvgRenderer(const QByteArray &contents, QObject *parent = nullptr)
Constructs a renderer with the given parent and loads the document in contents, as load() does.
QSvgRenderer::~QSvgRenderer()
Destroys the renderer.
bool QSvgRenderer::animated() const
Returns true if the document has animations. Nt SVG draws static documents and returns false.
int QSvgRenderer::animationDuration() const
Returns the number of frames of the animation, which is always 0.
Qt::AspectRatioMode QSvgRenderer::aspectRatioMode() const
Returns how the view box is fitted into the bounds of render(). The default is Qt::IgnoreAspectRatio: it is stretched to fill them.
See also setAspectRatioMode().
QRectF QSvgRenderer::boundsOnElement(const QString &id) const
Returns the bounding rectangle of the element with the given id, in the coordinates of its parent: the transformation of the element itself is applied, those of its ancestors are not. The stroke is part of the bounds. Returns a null rectangle if there is no such element or it has nothing to draw.
See also elementExists() and render().
int QSvgRenderer::currentFrame() const
Returns the current frame of the animation, which is always 0.
See also setCurrentFrame().
QSize QSvgRenderer::defaultSize() const
Returns the size the document asks for: its width and height, or the size of its view box where they are missing. Returns an invalid size if no document is loaded.
See also viewBoxF().
bool QSvgRenderer::elementExists(const QString &id) const
Returns true if the document has an element with the given id that can be drawn; otherwise returns false.
See also boundsOnElement().
int QSvgRenderer::framesPerSecond() const
Returns the number of frames per second of an animation. The default is 30.
See also setFramesPerSecond().
bool QSvgRenderer::isAnimationEnabled() const
Returns true if animations are enabled. The default is true.
See also setAnimationEnabled().
bool QSvgRenderer::isValid() const
Returns true if a document is loaded; otherwise returns false.
See also load().
bool QSvgRenderer::load(const QString &filename)
Loads the SVG file filename, which may be a resource (:/icons/logo.svg), and returns true on success. If the file cannot be read or is not an SVG document, the renderer becomes invalid and false is returned.
See also isValid().
bool QSvgRenderer::load(const QByteArray &contents)
This is an overloaded function.
Loads the SVG document in contents and returns true on success. The document must be well-formed XML in UTF-8 with an svg element as its root.
QtSvg::Options QSvgRenderer::options() const
Returns the options of the renderer.
See also setOptions().
void QSvgRenderer::render(QPainter *painter)
Draws the document with painter, filling the device it paints on.
void QSvgRenderer::render(QPainter *painter, const QRectF &bounds)
This is an overloaded function.
Draws the document with painter so that its view box fills bounds, as aspectRatioMode() says. Nothing is drawn outside bounds.
void QSvgRenderer::render(QPainter *painter, const QString &elementId, const QRectF &bounds = QRectF())
This is an overloaded function.
Draws the element with the id elementId, and what is in it, with painter. Its bounding rectangle (see boundsOnElement()) is stretched to fill bounds, or the whole device if bounds is empty. An element in defs can be drawn too.
void QSvgRenderer::repaintNeeded()
This signal is emitted when the document must be drawn again, because an animation moved on. Nt SVG has no animations and never emits it.
void QSvgRenderer::setAnimationEnabled(bool enable)
Enables animations if enable is true. The value is stored only, as there are no animations.
See also isAnimationEnabled().
void QSvgRenderer::setAspectRatioMode(Qt::AspectRatioMode mode)
Sets how the view box is fitted into the bounds of render() to mode. With Qt::KeepAspectRatio the document keeps its proportions and is centered in the bounds; with Qt::KeepAspectRatioByExpanding it covers them, and what does not fit is cut off.
See also aspectRatioMode().
void QSvgRenderer::setCurrentFrame(int frame)
Sets the current frame of the animation to frame. Does nothing, as there are no animations.
See also currentFrame().
void QSvgRenderer::setFramesPerSecond(int num)
Sets the number of frames per second to num. Negative numbers are ignored. The value is stored only, as there are no animations.
See also framesPerSecond().
void QSvgRenderer::setOptions(QtSvg::Options flags)
Sets the options to flags. QtSvg::AssumeTrustedSource lifts the limit on the number of elements that one drawing visits (500000, counting every use of an element), which keeps a hostile document from taking forever; the other options are stored and change nothing.
See also options().
void QSvgRenderer::setViewBox(const QRect &viewbox)
This is an overloaded function.
Sets the view box to viewbox.
void QSvgRenderer::setViewBox(const QRectF &viewbox)
Sets the view box to viewbox, so that render() shows that part of the document. Loading a document resets it.
See also viewBoxF().
QRect QSvgRenderer::viewBox() const
Returns the view box as a rectangle of integers.
See also viewBoxF().
QRectF QSvgRenderer::viewBoxF() const
Returns the view box: the part of the document that render() fits into its bounds. It is the viewBox of the document unless setViewBox() changed it; a document without one has a view box of its size, or the bounds of its contents if it has no size either.
See also setViewBox().