Nesta página

Renderização de arquivos SVG

Desenhos SVG podem ser renderizados em qualquer subclasse d QPaintDevice, como QWidget ou QImage. A maneira mais fácil de renderizar arquivos SVG é usar QSvgWidget ou QSvgRenderer.

Usando o QSvgWidget

QSvgWidget oferece um widget prático para exibir arquivos 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();
}

Você também pode carregar um arquivo SVG após a construção:

#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();
}

Usando o QSvgRenderer

Use QSvgRenderer para renderizar conteúdo SVG em qualquer 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;
}

Você também pode usar o ` QSvgRenderer ` para renderizar SVG diretamente no evento de pintura de um 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;
};

Essas abordagens permitem renderizar arquivos SVG em todos os dispositivos de pintura suportados pelo Qt, incluindo QWidget e QImage.

Opções de renderização

QSvgRenderer fornece opções de renderização options por meio da enumeração QtSvg::Option. Essas opções permitem que você controle como os arquivos SVG são analisados e renderizados.

Renderização de arquivos SVG animados

O módulo Qt SVG oferece suporte à renderização de arquivos SVG animados. Consulte a documentação da classe QSvgRenderer para obter detalhes sobre como trabalhar com SVGs animados.

Veja também Formatos de imagem vetorial no Qt e Recursos estendidos.

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