渲染 SVG 文件

Qt SVG 提供了用于渲染 SVG 文件的类。要包含模块类的定义,请使用以下指令:

#include <QtSvg>

要链接该模块,请在qmake .pro 文件中添加此行:

QT += svg

渲染 SVG 文件

可缩放矢量图形(SVG)是一种用 XML 描述二维图形和图形应用程序的语言。SVG 1.1 是 W3C 建议,是 Qt SVG 开发的核心。SVG 1.2 是SVG 工作组(SVG Working Group)目前正在开发的规范,其草案已经发布。移动 SVG 配置文件(SVG Basic 和 SVG Tiny)针对资源有限的设备,是第三代移动电话 3GPP 平台的一部分。有关 SVG 的更多信息,请访问About SVG

QtSVG 支持SVG 1.2 Tiny静态功能以及颜色和变换动画。目前不支持 ECMA 脚本和 DOM 操作。

自 Qt 6.7 起,支持SVG 1.1标准的一些附加功能

自 Qt 6.9 起,为填充、描边和变换添加了 CSS 动画,以匹配模块当前支持的 SMIL 动画。

SVG 图形可以渲染到任何QPaintDevice 子类上。这种方法为开发人员提供了试验的灵活性,以便为每个应用程序找到最佳解决方案。

渲染 SVG 文件最简单的方法是构建一个QSvgWidget ,然后使用QSvgWidget::load() 函数之一加载 SVG 文件。

QSvgRenderer 是负责为 渲染 SVG 文件的类,可直接用于为自定义 widget 提供 SVG 支持。要加载 SVG 文件,可使用文件名或文件内容构建 ,或在现有的渲染器上调用 () 。如果 SVG 文件已成功加载, () 将返回 true。QSvgWidget QSvgRenderer QSvgRenderer::load QSvgRenderer::isValid

成功加载 SVG 文件后,就可以使用QSvgRenderer::render() 函数对其进行渲染。请注意,该方案允许您在 Qt 支持的所有绘画设备上渲染 SVG 文件,包括QWidget 、QGLWidget 和QImage

© 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.