6.0.0-rc4Source

QSvgWidget Class

The QSvgWidget class is a widget that shows the contents of an SVG file. More…

Header#include <QSvgWidget>
CMaketarget_link_libraries(app PRIVATE Nt::SvgWidgets)
ModuleNt SVG Widgets C++ Classes
InheritsQWidget

List of all members, including inherited members

Public Functions

QSvgWidget(QWidget *parent = nullptr)
QSvgWidget(const QString &file, QWidget *parent = nullptr)
~QSvgWidget()
QSvgRenderer *renderer() const
QtSvg::Optionsoptions() const
voidsetOptions(QtSvg::Options options)

Reimplemented Public Functions

QSizesizeHint() const

Public Slots

voidload(const QString &file)
voidload(const QByteArray &contents)

Reimplemented Protected Functions

voidpaintEvent(QPaintEvent *event)

Detailed Description

The widget draws a document scaled to its own size, so it stays sharp at any size and on screens of any resolution:

QSvgWidget widget;
widget.renderer()->setAspectRatioMode(Qt::KeepAspectRatio);
if (argc > 1) {
    widget.load(QString::fromLocal8Bit(argv[1]));
} else {
    // No file: show what the generator writes, straight from memory.
    QBuffer buffer;
    QSvgGenerator generator;
    generator.setOutputDevice(&buffer);
    generator.setSize(QSize(240, 160));
    paintPicture(&generator);
    widget.load(buffer.data());
}
if (!widget.renderer()->isValid()) {
    std::fprintf(stderr, "svg_viewer: not an SVG document\n");
    return 1;
}
widget.setWindowTitle("Nt SVG");
widget.resize(widget.sizeHint());
widget.show();

Its size hint is the default size of the document. The widget does not keep the proportions of the document by itself; to do so, set the aspect ratio mode of its renderer():

widget->renderer()->setAspectRatioMode(Qt::KeepAspectRatio);

See also QSvgRenderer and QWidget.

Differences from Qt

Documents are static, as with QSvgRenderer, which lists what is drawn. There is no QGraphicsSvgItem, as Nt Widgets has no graphics view.

Member Function Documentation

[explicit] QSvgWidget::QSvgWidget(QWidget *parent = nullptr)

Constructs a widget without a document, with the given parent.

See also load().

[explicit] QSvgWidget::QSvgWidget(const QString &file, QWidget *parent = nullptr)

Constructs a widget with the given parent that shows the SVG file file.

[override] QSvgWidget::~QSvgWidget()

Destroys the widget.

[slot] void QSvgWidget::load(const QString &file)

Loads the SVG file file, which may be a resource, and shows it. If the file cannot be loaded the widget shows nothing; renderer()->isValid() tells.

[slot] void QSvgWidget::load(const QByteArray &contents)

This is an overloaded function.

Loads the SVG document in contents and shows it.

QtSvg::Options QSvgWidget::options() const

Returns the options of the renderer.

See also setOptions().

[override, protected] void QSvgWidget::paintEvent(QPaintEvent *event)

Reimplements QWidget::paintEvent().

Draws the document scaled to the widget. event is not used.

QSvgRenderer * QSvgWidget::renderer() const

Returns the renderer that draws the document. The widget owns it.

void QSvgWidget::setOptions(QtSvg::Options options)

Sets the options of the renderer to options.

See also options() and QSvgRenderer::setOptions().

[override] QSize QSvgWidget::sizeHint() const

Reimplements QWidget::sizeHint().

Returns the default size of the document, or 128 by 64 if none is loaded.