Representación de archivos SVG
Los dibujos SVG se pueden renderizar en cualquier subclase de ` QPaintDevice `, como ` QWidget ` o ` QImage`. La forma más sencilla de renderizar archivos SVG es utilizar ` QSvgWidget ` o ` QSvgRenderer`.
Uso de QSvgWidget
QSvgWidget ofrece un widget muy práctico para visualizar archivos SVG.
#include <QApplication>
#include <QSvgWidget>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QSvgWidget svgWidget(QStringLiteral(":/images/example.svg"));
svgWidget.setGeometry(100, 100, 400, 400);
svgWidget.show();
return app.exec();
}También puedes cargar un archivo SVG tras la creación:
#include <QApplication>
#include <QSvgWidget>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QSvgWidget *svgWidget = new QSvgWidget;
svgWidget->load(QStringLiteral(":/images/example.svg"));
svgWidget->show();
return app.exec();
}Uso de QSvgRenderer
Utiliza QSvgRenderer para renderizar contenido SVG en cualquier QPaintDevice.
#include <QApplication>
#include <QSvgRenderer>
#include <QImage>
#include <QPainter>
#include <QLabel>
#include <QPixmap>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QSvgRenderer renderer(QStringLiteral(":/images/example.svg"));
if (renderer.isValid()) {
QImage image(400, 400, QImage::Format_ARGB32_Premultiplied);
image.fill(Qt::transparent);
QPainter painter(&image);
renderer.render(&painter);
QLabel label;
label.setPixmap(QPixmap::fromImage(image));
label.resize(400, 400);
label.show();
return app.exec();
}
return 1;
}También puedes utilizar ` QSvgRenderer ` para renderizar SVG directamente en el evento de pintura de un widget personalizado:
// In your widget's header file:
#include <QSvgRenderer>
class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget(QWidget *parent = nullptr)
: QWidget(parent), renderer(QStringLiteral(":/images/example.svg")) {}
protected:
void paintEvent(QPaintEvent *) override {
QPainter painter(this);
renderer.render(&painter);
}
private:
QSvgRenderer renderer;
};Estos métodos te permiten renderizar archivos SVG en todos los dispositivos de pintura compatibles con Qt, incluidos QWidget y QImage.
Opciones de representación
QSvgRenderer Ofrece opciones de renderización options a través de la enumeración QtSvg::Option. Estas opciones te permiten controlar cómo se analizan y se renderizan los archivos SVG.
Representación de archivos SVG animados
El módulo Qt SVG admite la representación de archivos SVG animados. Consulte la documentación de la clase QSvgRenderer para obtener más detalles sobre cómo trabajar con SVG animados.
Véase también «Formatos de imagen vectorial en Qt » y «Funciones ampliadas».
© 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.