In questa pagina

Rendering dei file SVG

I disegni SVG possono essere renderizzati su qualsiasi sottoclasse di ` QPaintDevice `, come ` QWidget ` o ` QImage`. Il modo più semplice per renderizzare i file SVG è utilizzare ` QSvgWidget ` o ` QSvgRenderer`.

Utilizzo di QSvgWidget

QSvgWidget fornisce un comodo widget per la visualizzazione dei file 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();
}

È inoltre possibile caricare un file SVG dopo la creazione:

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

Utilizzo di QSvgRenderer

Utilizzare QSvgRenderer per visualizzare contenuti SVG su qualsiasi 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;
}

È inoltre possibile utilizzare QSvgRenderer per renderizzare SVG direttamente nell'evento paint di un widget personalizzato:

// 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;
};

Questi approcci consentono di visualizzare file SVG su tutti i dispositivi di disegno supportati da Qt, inclusi QWidget e QImage.

Opzioni di rendering

QSvgRenderer fornisce opzioni di rendering per options tramite l’enumerazione QtSvg::Option. Queste opzioni consentono di controllare il modo in cui i file SVG vengono analizzati e visualizzati.

Rendering di file SVG animati

Il modulo Qt SVG supporta il rendering di file SVG animati. Per ulteriori dettagli su come lavorare con gli SVG animati, consultare la documentazione della classe QSvgRenderer.

Vedi anche Formati di immagini vettoriali in Qt e Funzionalità estese.

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