Important: Please read the Qt Code of Conduct -

Adding attributes in SVG file

  • Hi,
    I am working on the task for saving the complete scene as SVG. I need to add the attributes like Item id and object class name for the each individual items in the scene while generating the svg files.

    But I notice that there is no predefined QT API's to do this task. Could anybody share some insight on this how to add attributes while generating svg file for the scene?

    I was able to save the complete scene as svg by using below code

    QString path;
    QString newPath = QFileDialog::getSaveFileName(this, tr("Save SVG"),
                                                   path, tr("SVG files (*.svg)"));
    if (newPath.isEmpty())
    path = newPath;
    QSvgGenerator generator;
    generator.setSize(QSize(200, 200));
    generator.setViewBox(QRect(0, 0, 200, 200));
    generator.setTitle(tr("SVG Generator Example Drawing"));
    generator.setDescription(tr("An SVG drawing created by the SVG Generator "
                                "Example provided with Qt."));
    QPainter painter;
    scene->render( &painter );

    Thank you in advance

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    AFAIK, there's currently nothing for that in the generator API.

    One possibility would be to add an adequate API to the class.

    Otherwise, SVG being xml, you may also just edit the resulting file using QXmlStreamReader/QXmlStreamWriter and update it in a second step.

  • @SGaist Hi Gaist,
    Thank you for the suggestion. I tried the first possibility to add API to the class QSvgGenerator.

    As this class is based on PIMPL Design(D-Pointer), I tried to do as below

    //Note: svggencustom header file

    #ifndef SVGGENCUSTOM_H
    #define SVGGENCUSTOM_H

    #include "qsvggenerator.h"
    #include "qglobal.h"

    class SvgGenCustomPrivate;

    class SvgGenCustom : public QSvgGenerator
    ~SvgGenCustom() override;
    void setFoo(int);
    void showOutputFoo();

    #endif // SVGGENCUSTOM_H

    //Note: svggencustom source file

    #include "svggencustom.h"
    #include "svggencustom_p.h"
    #include <QDebug>

    SvgGenCustom::SvgGenCustom() : QSvgGenerator(*new SvgGenCustom)




    void SvgGenCustom::setFoo(int)
    d->foo = 11;


    void SvgGenCustom::showOutputFoo()
    qInfo() << d->foo <<endl;

    //Note: SvgGenCustomPrivate Private Header File
    //Here I was not able to inherit QSvgGeneratorPrivate class as it is not defined in separate header file
    //No #include QSvgGeneratorPrivate header file

    #ifndef SVGGENCUSTOM_P_H
    #define SVGGENCUSTOM_P_H

    class QSvgGeneratorPrivate;
    class SvgGenCustomPrivate : public QSvgGeneratorPrivate{
    int foo;

    #endif // SVGGENCUSTOM_P_H

    I was getting the error as the expression QScopedPointer<QSvgGeneratorPrivate> d_ptr; in QSvgGenerator header file is in Private Access modifier and QSvgGeneratorPrivate class not able to subclass as it is defined in the source file instead in separate header file.

    Kindly, could you provide few more possibility for creating custom svg generator in QT.

    Thank you in advance.

  • Lifetime Qt Champion

    My suggestion was not to subclass but to actually add the feature to the class.

Log in to reply