QGraphicsSvgItem Class

QGraphicsSvgItem 类是一个QGraphicsItem ,可用于渲染 SVG 文件的内容。更多

头文件: #include <QGraphicsSvgItem>
CMake: find_package(Qt6 REQUIRED COMPONENTS SvgWidgets)
target_link_libraries(mytarget PRIVATE Qt6::SvgWidgets)
qmake: QT += svgwidgets
继承: QGraphicsObject

属性

公共函数

QGraphicsSvgItem(QGraphicsItem *parent = nullptr)
QGraphicsSvgItem(const QString &fileName, QGraphicsItem *parent = nullptr)
QString elementId() const
QSize maximumCacheSize() const
QSvgRenderer *renderer() const
void setElementId(const QString &id)
void setMaximumCacheSize(const QSize &size)
void setSharedRenderer(QSvgRenderer *renderer)

重新实现的公共函数

virtual QRectF boundingRect() const override
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override
virtual int type() const override

详细说明

QGraphicsSvgItem 提供了一种将 SVG 文件渲染到QGraphicsView 的方法。QGraphicsSvgItem 可通过向其构造函数传递要渲染的 SVG 文件或通过显式设置共享QSvgRenderer 来创建。

请注意,在 QGraphicsSvgItem 上设置QSvgRenderer 并不会使该项目获得渲染器的所有权,因此如果使用setSharedRenderer() 方法,必须确保QSvgRenderer 对象的生命周期至少与 QGraphicsSvgItem 的生命周期一样长。

QGraphicsSvgItem 提供了一种通过setElementId 只渲染 SVG 文件部分内容的方法。如果调用setElementId() 方法,只有传入 id 的 SVG 元素(及其子元素)才会被渲染。这为有选择地渲染包含大量离散元素的大型 SVG 文件提供了方便。例如,以下代码只渲染包含整副扑克牌的 SVG 文件中的小丑:

QSvgRenderer *renderer = new QSvgRenderer(QLatin1String("SvgCardDeck.svg"));
QGraphicsSvgItem *black = new QGraphicsSvgItem();
QGraphicsSvgItem *red   = new QGraphicsSvgItem();

black->setSharedRenderer(renderer);
black->setElementId(QLatin1String("black_joker"));

red->setSharedRenderer(renderer);
red->setElementId(QLatin1String("red_joker"));

可以通过直接操作项目变换矩阵来设置项目的大小。

默认情况下,SVG 渲染使用QGraphicsItem::DeviceCoordinateCache 模式缓存,以加快项目的显示速度。可以通过向QGraphicsItem::setCacheMode() 方法传递QGraphicsItem::NoCache 来禁用缓存。

另请参见 QSvgWidgetQt SVG C++ Classes,QGraphicsItem, 和QGraphicsView

属性文档

elementId : QString

该属性包含元素的 XML ID。

访问功能

QString elementId() const
void setElementId(const QString &id)

maximumCacheSize : QSize

此属性保存此项目的设备坐标缓存的最大大小。

访问函数:

QSize maximumCacheSize() const
void setMaximumCacheSize(const QSize &size)

成员函数文档

QGraphicsSvgItem::QGraphicsSvgItem(QGraphicsItem *parent = nullptr)

用给定的parent 构建一个新的 SVG 项目。

QGraphicsSvgItem::QGraphicsSvgItem(const QString &fileName, QGraphicsItem *parent = nullptr)

用给定的parent 构建一个新项目,并用指定的fileName 加载 SVG 文件的内容。

[override virtual] QRectF QGraphicsSvgItem::boundingRect() const

重实现:QGraphicsItem::boundingRect() 常量。

返回此项目的边界矩形。

QString QGraphicsSvgItem::elementId() const

返回当前正在渲染的元素的 XML ID。如果正在渲染整个文件,则返回空字符串。

注: 属性 elementId 的获取函数。

另请参阅 setElementId().

QSize QGraphicsSvgItem::maximumCacheSize() const

返回此项目设备坐标缓存的当前最大大小。如果项目是使用QGraphicsItem::DeviceCoordinateCache 模式缓存的,那么如果项目在设备坐标中的扩展名大于最大尺寸,缓存将被绕过。默认最大缓存尺寸为 1024x768。

默认的最大缓存大小为 1024x768。QPixmapCache::cacheLimit() 给出了整个缓存的累积边界,而 maximumCacheSize() 则是指该特定项目的最大缓存大小。

注: 属性 maximumCacheSize 的获取函数。

另请参阅 setMaximumCacheSize() 和QGraphicsItem::cacheMode()。

[override virtual] void QGraphicsSvgItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr)

重实现:QGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)。

QSvgRenderer *QGraphicsSvgItem::renderer() const

返回当前使用的QSvgRenderer

void QGraphicsSvgItem::setElementId(const QString &id)

将元素的 XML ID 设置为id

注: 属性elementId 的设置函数。

另请参见 elementId().

void QGraphicsSvgItem::setMaximumCacheSize(const QSize &size)

将项目的最大设备坐标缓存大小设置为size 。如果使用QGraphicsItem::DeviceCoordinateCache 模式缓存项目,则如果项目在设备坐标中的扩展名大于size ,缓存将被绕过。

缓存与用于缓存渲染结果的QPixmap 相对应。使用QPixmapCache::setCacheLimit() 对整个缓存设置限制,为单个项目设置缓存大小时使用 setMaximumCacheSize()。

注: 属性maximumCacheSize 的设置函数。

另请参阅 maximumCacheSize() 和QGraphicsItem::cacheMode()。

void QGraphicsSvgItem::setSharedRenderer(QSvgRenderer *renderer)

renderer 设置为项目上的共享QSvgRenderer 。使用这种方法,可以在多个项目上共享同一个QSvgRenderer 。这意味着 SVG 文件将只被解析一次。只要该项目还在使用,传递给此方法的QSvgRenderer 就必须存在。

[override virtual] int QGraphicsSvgItem::type() const

重实现:QGraphicsItem::type() const.

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.