QGraphicsSvgItem Class▲
-
Header: QGraphicsSvgItem
-
CMake:
find_package(Qt6 REQUIRED COMPONENTS SvgWidgets)
target_link_libraries(mytarget PRIVATE Qt6::SvgWidgets)
-
qmake: QT += svgwidgets
-
Inherits: QGraphicsObject
-
Group: QGraphicsSvgItem is part of graphicsview-api
Detailed Description▲
QGraphicsSvgItem provides a way of rendering SVG files onto QGraphicsView. QGraphicsSvgItem can be created by passing the SVG file to be rendered to its constructor or by explicit setting a shared QSvgRenderer on it.
Note that setting QSvgRenderer on a QGraphicsSvgItem doesn't make the item take ownership of the renderer, therefore if using setSharedRenderer() method one has to make sure that the lifetime of the QSvgRenderer object will be at least as long as that of the QGraphicsSvgItem.
QGraphicsSvgItem provides a way of rendering only parts of the SVG files via the setElementId. If setElementId() method is called, only the SVG element (and its children) with the passed id will be renderer. This provides a convenient way of selectively rendering large SVG files that contain a number of discrete elements. For example the following code renders only jokers from a SVG file containing a whole card deck:
QSvgRenderer *
renderer =
new
QSvgRenderer(QLatin1String("SvgCardDeck.svg"
));
QGraphicsSvgItem *
black =
new
QGraphicsSvgItem();
QGraphicsSvgItem *
red =
new
QGraphicsSvgItem();
black-&
gt;setSharedRenderer(renderer);
black-&
gt;setElementId(QLatin1String("black_joker"
));
red-&
gt;setSharedRenderer(renderer);
red-&
gt;setElementId(QLatin1String("red_joker"
));
Size of the item can be set via direct manipulation of the items transformation matrix.
By default the SVG rendering is cached using QGraphicsItem::DeviceCoordinateCache mode to speedup the display of items. Caching can be disabled by passing QGraphicsItem::NoCache to the QGraphicsItem::setCacheMode() method.
See Also▲
See also QSvgWidget, Qt SVG C++ Classes, QGraphicsItem, QGraphicsView
Property Documentation▲
elementId : QString▲
This property holds the element's XML ID.
Access functions:
-
elementId() const
-
void setElementId(const &id)
maximumCacheSize : QSize▲
This property holds the maximum size of the device coordinate cache for this item.
Access functions:
-
maximumCacheSize() const
-
void setMaximumCacheSize(const &size)
Member Function Documentation▲
QGraphicsSvgItem::QGraphicsSvgItem(QGraphicsItem *parent = nullptr)▲
Constructs a new SVG item with the given parent.
QGraphicsSvgItem::QGraphicsSvgItem(const QString &fileName, QGraphicsItem *parent = nullptr)▲
Constructs a new item with the given parent and loads the contents of the SVG file with the specified fileName.
[override virtual] QRectF QGraphicsSvgItem::boundingRect() const▲
Reimplements: QGraphicsItem::boundingRect() const.
Returns the bounding rectangle of this item.
QString QGraphicsSvgItem::elementId() const▲
Returns the XML ID the element that is currently being rendered. Returns an empty string if the whole file is being rendered.
Getter function for property elementId.
See Also▲
See also setElementId()
QSize QGraphicsSvgItem::maximumCacheSize() const▲
Returns the current maximum size of the device coordinate cache for this item. If the item is cached using QGraphicsItem::DeviceCoordinateCache mode, caching is bypassed if the extension of the item in device coordinates is larger than the maximum size.
The default maximum cache size is 1024x768. QPixmapCache::cacheLimit() gives the cumulative bounds of the whole cache, whereas maximumCacheSize() refers to a maximum cache size for this particular item.
Getter function for property maximumCacheSize.
See Also▲
See also setMaximumCacheSize(), QGraphicsItem::cacheMode()
[override virtual] void QGraphicsSvgItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr)▲
Reimplements: QGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget).
QSvgRenderer *QGraphicsSvgItem::renderer() const▲
Returns the currently use QSvgRenderer.
void QGraphicsSvgItem::setElementId(const QString &id)▲
Sets the XML ID of the element to id.
Setter function for property elementId.
See Also▲
See also elementId()
void QGraphicsSvgItem::setMaximumCacheSize(const QSize &size)▲
Sets the maximum device coordinate cache size of the item to size. If the item is cached using QGraphicsItem::DeviceCoordinateCache mode, caching is bypassed if the extension of the item in device coordinates is larger than size.
The cache corresponds to the QPixmap which is used to cache the results of the rendering. Use QPixmapCache::setCacheLimit() to set limitations on the whole cache and use setMaximumCacheSize() when setting cache size for individual items.
Setter function for property maximumCacheSize.
See Also▲
See also maximumCacheSize(), QGraphicsItem::cacheMode()
void QGraphicsSvgItem::setSharedRenderer(QSvgRenderer *renderer)▲
[override virtual] int QGraphicsSvgItem::type() const▲
Reimplements: QGraphicsItem::type() const.
Obsolete Members for QGraphicsSvgItem▲
The following members of class QGraphicsSvgItem are deprecated. We strongly advise against using them in new code.
Obsolete Member Function Documentation▲
bool QGraphicsSvgItem::isCachingEnabled() const▲
This function is deprecated. We strongly advise against using it in new code.
Use QGraphicsItem::cacheMode() instead.
void QGraphicsSvgItem::setCachingEnabled(bool caching)▲
This function is deprecated. We strongly advise against using it in new code.
Use QGraphicsItem::setCacheMode() instead. Passing true to this function is equivalent to QGraphicsItem::setCacheMode(QGraphicsItem::DeviceCoordinateCache).
See Also▲
See also isCachingEnabled()