QSvgGenerator Class
QSvgGenerator 类提供了一个绘图设备,用于创建 SVG 绘图。更多内容...
| 头文件: | #include <QSvgGenerator> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Svg) target_link_libraries(mytarget PRIVATE Qt6::Svg) |
| qmake: | QT += svg |
| 继承自: | QPaintDevice |
- 所有成员列表(包括继承的成员)
- QSvgGenerator 属于绘图类。
注意:该类中的所有函数均为可重入的。
公共类型
(since 6.5) enum class | SvgVersion { SvgTiny12, Svg11 } |
属性
|
公共函数
| QSvgGenerator() | |
(since 6.5) | QSvgGenerator(QSvgGenerator::SvgVersion version) |
| virtual | ~QSvgGenerator() |
| QString | description() const |
| QString | fileName() const |
| QIODevice * | outputDevice() const |
| int | resolution() const |
| void | setDescription(const QString &description) |
| void | setFileName(const QString &fileName) |
| void | setOutputDevice(QIODevice *outputDevice) |
| void | setResolution(int dpi) |
| void | setSize(const QSize &size) |
| void | setTitle(const QString &title) |
| void | setViewBox(const QRect &viewBox) |
| void | setViewBox(const QRectF &viewBox) |
| QSize | size() const |
(since 6.5) QSvgGenerator::SvgVersion | svgVersion() const |
| QString | title() const |
| QRect | viewBox() const |
| QRectF | viewBoxF() const |
重新实现的受保护函数
(since 6.11) virtual void | initPainter(QPainter *painter) const override |
| virtual int | metric(QPaintDevice::PaintDeviceMetric metric) const override |
| virtual QPaintEngine * | paintEngine() const override |
详细说明
此绘图设备表示可缩放矢量图形(SVG)绘图。与 QPrinter 一样,它被设计为只写设备,用于生成特定格式的输出。
要写入 SVG 文件,首先需要通过设置 `fileName ` 或 `outputDevice ` 属性来配置输出。通常需要通过设置 `size ` 属性来指定绘图的大小;在某些情况下,如果该绘图将被包含在另一个绘图中,则还需要设置 `viewBox ` 属性。
QSvgGenerator generator;
generator.setFileName(path);
generator.setSize(QSize(200, 200));
generator.setViewBox(QRect(0, 0, 200, 200));
generator.setTitle(tr("SVG Generator Drawing"));
generator.setDescription(tr("An SVG drawing created by the SVG Generator"));其他元数据可通过设置title 、description 和resolution 属性来指定。
与其他 `QPaintDevice ` 子类一样,需要使用 `QPainter ` 对象来对该类的实例进行绘制:
QPainter painter;
painter.begin(&generator);
...
painter.end();绘制操作与任何其他绘图设备的方式相同。但是,必须使用QPainter::begin() 和end() 方法来显式地开始和结束该设备上的绘制操作。
另请参阅 QSvgRenderer 、QSvgWidget 以及 Qt SVG C++ Classes。
成员类型文档
[since 6.5] enum class QSvgGenerator::SvgVersion
此枚举描述了生成器的 SVG 输出版本。
| 常量 | 值 | 描述 |
|---|---|---|
QSvgGenerator::SvgVersion::SvgTiny12 | 0 | 生成的文档遵循 SVG Tiny 1.2 规范。 |
QSvgGenerator::SvgVersion::Svg11 | 1 | 生成的文档遵循 SVG 1.1 规范。 |
该枚举类型在 Qt 6.5 中引入。
属性文档
description : QString
该属性包含生成的 SVG 绘图的描述
访问函数:
| QString | description() const |
| void | setDescription(const QString &description) |
另请参阅 title 。
fileName : QString
该属性存储生成的 SVG 绘图的目标文件名
访问函数:
| QString | fileName() const |
| void | setFileName(const QString &fileName) |
另请参阅 outputDevice 。
outputDevice : QIODevice*
该属性存储生成的 SVG 绘图的输出设备
如果同时指定了输出设备和文件名,则输出设备具有优先级。
访问函数:
| QIODevice * | outputDevice() const |
| void | setOutputDevice(QIODevice *outputDevice) |
另请参阅 fileName 。
resolution : int
该属性存储生成的输出分辨率
分辨率以每英寸点数(dpi)为单位指定,用于计算 SVG 绘图的物理尺寸。
访问函数:
| int | resolution() const |
| void | setResolution(int dpi) |
size : QSize
该属性存储生成的 SVG 绘图的大小
默认情况下,该属性设置为QSize(-1, -1) ,这表示生成器不应输出<svg> 元素的width和height属性。
注意: 当生成器上处于QPainter 状态时,无法更改此属性。
访问函数:
| QSize | size() const |
| void | setSize(const QSize &size) |
另请参阅 ` viewBox ` 和 `resolution`。
title : QString
该属性保存生成的 SVG 绘图的标题
访问函数:
| QString | title() const |
| void | setTitle(const QString &title) |
另请参阅 description 。
viewBox : QRectF
该属性存储生成的 SVG 绘图的 viewBox
默认情况下,该属性设置为QRect(0, 0, -1, -1) ,这表示生成器不应输出<svg> 元素的viewBox属性。
注意: 当生成器上处于QPainter 状态时,无法更改此属性。
访问函数:
| QRectF | viewBoxF() const |
| void | setViewBox(const QRect &viewBox) |
| void | setViewBox(const QRectF &viewBox) |
另请参阅 viewBox()、size 以及resolution 。
成员函数文档
QSvgGenerator::QSvgGenerator()
使用 SVG Tiny 1.2 配置文件构建一个新的生成器。
[explicit, since 6.5] QSvgGenerator::QSvgGenerator(QSvgGenerator::SvgVersion version)
创建一个新的生成器,该生成器使用 SVG 版本version 。
该函数于 Qt 6.5 中引入。
[virtual noexcept] QSvgGenerator::~QSvgGenerator()
摧毁发电机。
[override virtual protected, since 6.11] void QSvgGenerator::initPainter(QPainter *painter) const
该函数是在 Qt 6.11 中引入的。
[override virtual protected] int QSvgGenerator::metric(QPaintDevice::PaintDeviceMetric metric) const
重写了:QPaintDevice::metric (QPaintDevice::PaintDeviceMetric metric)const。
[override virtual protected] QPaintEngine *QSvgGenerator::paintEngine() const
重写:QPaintDevice::paintEngine() const。
返回用于渲染待转换为 SVG 格式的图形所使用的绘图引擎信息。
[since 6.5] QSvgGenerator::SvgVersion QSvgGenerator::svgVersion() const
返回该生成器正在生成的 SVG 文档的版本。
该函数于 Qt 6.5 中引入。
QRect QSvgGenerator::viewBox() const
返回viewBoxF().toRect()。
另请参阅 setViewBox() 和viewBoxF()。
© 2026 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.