SVGファイルのレンダリング
SVG 図形は、QWidget やQImage など、QPaintDevice のサブクラスであればどれにでも描画できます。SVG ファイルを描画する最も簡単な方法は、QSvgWidget またはQSvgRenderer を使用することです。
QSvgWidgetの使用
QSvgWidget を使用すると、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();
}また、インスタンス生成後にSVGファイルをロードすることも可能です:
#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();
}QSvgRendererの使用
QSvgRenderer を使用すると、任意のQPaintDevice 上に SVG コンテンツをレンダリングできます。
#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;
}また、QSvgRenderer を使用して、カスタムウィジェットのペイントイベント内で SVG を直接レンダリングすることもできます:
// 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;
};これらの方法により、QWidget やQImage など、Qtがサポートするすべてのペイントデバイス上でSVGファイルをレンダリングできます。
レンダリングオプション
QSvgRenderer は、QtSvg::Option 列挙型を通じてoptions のレンダリングオプションを提供します。これらのオプションを使用すると、SVGファイルの解析およびレンダリング方法を制御できます。
アニメーション付きSVGファイルのレンダリング
Qt SVG モジュールは、アニメーション付きSVGファイルのレンダリングをサポートしています。アニメーション付きSVGの取り扱い方法の詳細については、QSvgRenderer クラスのドキュメントを参照してください。
「Qt Image Formats」および「拡張機能」も参照してください 。
© 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.