Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Adding attributes in SVG file
Forum Updated to NodeBB v4.3 + New Features

Adding attributes in SVG file

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 437 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dinesh Prasad
    wrote on last edited by Dinesh Prasad
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      D 1 Reply Last reply
      5
      • SGaistS SGaist

        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.

        D Offline
        D Offline
        Dinesh Prasad
        wrote on last edited by Dinesh Prasad
        #3

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved