Renderización de archivos SVG
Los dibujos SVG pueden renderizarse en cualquier subclase de QPaintDevice, como QWidget o QImage. La forma más sencilla de renderizar archivos SVG es utilizar QSvgWidget o QSvgRenderer.
El uso de QSvgWidget
QSvgWidget proporciona un práctico widget para mostrar 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 puede cargar un archivo SVG después de la construcció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(); }
Utilizar QSvgRenderer
Utilice 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 puede utilizar QSvgRenderer para renderizar SVG directamente en el evento paint 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 enfoques le permiten renderizar archivos SVG en todos los dispositivos de pintura soportados por Qt, incluyendo QWidget y QImage.
Opciones de renderizado
QSvgRenderer proporciona renderizado options a través del enum QtSvg::Option. Estas opciones permiten controlar cómo se analizan y renderizan los archivos SVG.
Renderización de archivos SVG animados
El módulo Qt SVG permite renderizar archivos SVG animados. Consulte la documentación de la clase QSvgRenderer para obtener más información sobre cómo trabajar con archivos SVG animados.
Véase también Vector Image Formats in Qt y Extended Features.
© 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.