Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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?

    Note:
    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())
        return;
    
    path = newPath;
    
    QSvgGenerator generator;
    generator.setFileName(path);
    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;
    painter.begin(&generator);
    scene->render( &painter );
    painter.end();
    

    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
    {
    Q_DECLARE_PRIVATE(SvgGenCustom)
    public:
    SvgGenCustom();
    ~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)
    {

    }

    SvgGenCustom::~SvgGenCustom()
    {

    }

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

    }

    void SvgGenCustom::showOutputFoo()
    {
    Q_D(SvgGenCustom);
    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{
    public:
    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