Evgenii Legotckoi
April 1, 2016, 10:51 p.m.

Qt/C++ - Lesson 045. SvgReader on the Qt. Loading data from SVG file into QGraphicsScene

In the last article I was considered an example of how to save objects in a graphical scene SVG file, and then we were able to open it in CorelDraw. Now try the same file open and restore the graphics in QGraphicsScene .

Note that we will not use QSvgRenderer class for this, for the reason that it is easily put the contents of SVG files in the graphic scene, but it will be one single graphic, but if you need to have it restored as a separate graphic objects, such as , QGraphicsItem , you will need to parse the SVG file to make of it all graphical objects.

Since SVG file is of XML-format structure, then disassemble it does not present any difficulty using classes QDomDocument family.

Структура проекта


As an example, the draft of the preceding article to be used, but will be diluted with additional class two static methods.

  • SVGExample.pro - the profile of the project;
  • svgreader.h - header parsing SVG file;
  • svgreader.cpp - source SVG file parser;
  • mainwindow.h - header file of the main application window;
  • mainwindow.cpp - file source code of the main application window;
  • mainwindow.ui - file forms the main application window;
  • main.cpp - start file the application source code.

Structure of SVG file

Because we need to parse the file, look at his insides.

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <svg width="141.111mm" height="141.111mm"
  3. viewBox="0 0 400 400"
  4. xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.2" baseProfile="tiny">
  5. <title>SVG Example</title>
  6. <desc>File created by SVG Example</desc>
  7. <defs>
  8. </defs>
  9. <g fill="none" stroke="black" stroke-width="1" fill-rule="evenodd" stroke-linecap="square" stroke-linejoin="bevel" >
  10.  
  11. <g fill="#ff0000" fill-opacity="1" stroke="#000000" stroke-opacity="1" stroke-width="2" stroke-linecap="square" stroke-linejoin="bevel" transform="matrix(1,0,0,1,0,0)"
  12. font-family="MS Shell Dlg 2" font-size="8.25" font-weight="400" font-style="normal"
  13. >
  14. <rect x="10" y="50" width="100" height="50"/>
  15. </g>
  16. </g>

So, we are interested in svg tag, g, rect . The tag contains svg the size of the graphic scenes - it viewBox. The tag contains a rectangle rect size and the tag g, which wrapped rect tag contains the fill color and outline of a rectangle, and the thickness of the outline. They will need and Spars.

mainwindow.ui

This file will be added when the button load, on a signal from which the open file dialog and launch the opening of our SVG file.

SVGExample.pro

I call your attention to the fact that the profile of the project will connect the module xml need.

  1. QT += xml

svgreader.h

  1. #ifndef SVGREADER_H
  2. #define SVGREADER_H
  3.  
  4. #include <QList>
  5. #include <QGraphicsRectItem>
  6.  
  7. class SvgReader
  8. {
  9. public:
  10. SvgReader();
  11. static QList<QGraphicsRectItem *> getElements(const QString filename);
  12. static QRectF getSizes(const QString filename);
  13. };
  14.  
  15. #endif // SVGREADER_H

svgreader.cpp

  1. #include "svgreader.h"
  2. #include <QPen>
  3. #include <QFile>
  4. #include <QMessageBox>
  5. #include <QDomDocument>
  6. #include <QStringList>
  7.  
  8. SvgReader::SvgReader()
  9. {
  10.  
  11. }
  12.  
  13. QList<QGraphicsRectItem *> SvgReader::getElements(const QString filename)
  14. {
  15. QList<QGraphicsRectItem *> rectList; // Declare the stack list boxes
  16.  
  17. QDomDocument doc; // document object
  18. QFile file(filename); // Open your SVG-file
  19. // If it is not opened, or have failed to transmit the contents in QDocDocument
  20. if (!file.open(QIODevice::ReadOnly) || !doc.setContent(&file))
  21. return rectList; // the refund list, but empty
  22.  
  23. // We are looking for all objects in a document with tag g
  24. QDomNodeList gList = doc.elementsByTagName("g");
  25. // Getting them to touch
  26. for (int i = 0; i < gList.size(); i++) {
  27. QDomNode gNode = gList.item(i); // Select from the node list
  28. QDomElement rectangle = gNode.firstChildElement("rect"); // And we seek in her element c rect tag
  29. // If the resulting elements are not zero, then
  30. if (rectangle.isNull()){
  31. continue;
  32. } else {
  33. // We begin to create a rectangle
  34. QGraphicsRectItem *rect = new QGraphicsRectItem();
  35. // This flag makes the object moves, the need to test
  36. rect->setFlag(QGraphicsItem::ItemIsMovable);
  37. // Taking the dimensions of rect tag
  38. QDomElement gElement = gNode.toElement();
  39. rect->setRect(rectangle.attribute("x").toInt(),
  40. rectangle.attribute("y").toInt(),
  41. rectangle.attribute("width").toInt(),
  42. rectangle.attribute("height").toInt());
  43.  
  44. /* Take away from the element node gNode color options
  45.              * Yes yes yes ... because of gNode, instead of the rectangle. These parameters are stored in the tag g
  46. * */
  47. QColor fillColor(gElement.attribute("fill", "#ffffff"));
  48. fillColor.setAlphaF(gElement.attribute("fill-opacity","0").toFloat());
  49. rect->setBrush(QBrush(fillColor));
  50.  
  51. QColor strokeColor(gElement.attribute("stroke", "#000000"));
  52. strokeColor.setAlphaF(gElement.attribute("stroke-opacity").toFloat());
  53.  
  54. rect->setPen(QPen(strokeColor,gElement.attribute("stroke-width", "0").toInt()));
  55. rectList.append(rect);
  56. }
  57. }
  58. file.close();
  59. return rectList;
  60. }
  61.  
  62. QRectF SvgReader::getSizes(const QString filename)
  63. {
  64. QDomDocument doc; // initialize the stack object QDomDocument
  65. QFile file(filename); // Open your SVG-file
  66. // If it is not opened, or have failed to transmit the contents in QDocDocument
  67. if (!file.open(QIODevice::ReadOnly) || !doc.setContent(&file))
  68. return QRectF(0,0,200,200); // the return values for the default scene
  69.  
  70. /* Then pick up a list of items with the tag svg.
  71.       * If a list of elements is not empty,
  72.       * take the size of the graphic scenes.
  73. * */
  74. QDomNodeList list = doc.elementsByTagName("svg");
  75. if(list.length() > 0) {
  76. QDomElement svgElement = list.item(0).toElement();
  77. QStringList parameters = svgElement.attribute("viewBox").split(" ");
  78. return QRectF(parameters.at(0).toInt(),
  79. parameters.at(1).toInt(),
  80. parameters.at(2).toInt(),
  81. parameters.at(3).toInt());
  82. }
  83. return QRectF(0,0,200,200);
  84. }

mainwindow.h

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3.  
  4. #include <QMainWindow>
  5. #include <QGraphicsScene>
  6. #include <QGraphicsRectItem>
  7. #include <QSvgGenerator>
  8. #include <QFileDialog>
  9. #include <QPainter>
  10.  
  11. namespace Ui {
  12. class MainWindow;
  13. }
  14.  
  15. class MainWindow : public QMainWindow
  16. {
  17. Q_OBJECT
  18.  
  19. public:
  20. explicit MainWindow(QWidget *parent = 0);
  21. ~MainWindow();
  22.  
  23. private slots:
  24. void on_saveButton_clicked();
  25. void on_loadButton_clicked();
  26.  
  27. private:
  28. Ui::MainWindow *ui;
  29. QGraphicsScene *scene;
  30. QString path; // The path to save the file
  31. };
  32.  
  33. #endif // MAINWINDOW_H

mainwindow.cpp

Here in this file only the contents directly related to the analysis of the SVG file. The pressing processing slot button is selected using a file dialog box, and is broken down into graphical objects.

  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include "svgreader.h"
  4.  
  5. MainWindow::MainWindow(QWidget *parent) :
  6. QMainWindow(parent),
  7. ui(new Ui::MainWindow)
  8. {
  9. ui->setupUi(this);
  10.  
  11. scene = new QGraphicsScene();
  12. ui->graphicsView->setScene(scene);
  13. scene->setSceneRect(0,0,400,400);
  14. }
  15.  
  16. MainWindow::~MainWindow()
  17. {
  18. delete ui;
  19. }
  20.  
  21. void MainWindow::on_saveButton_clicked()
  22. {
  23. // The code from the previous tutorial on working with SVG
  24. }
  25.  
  26. void MainWindow::on_loadButton_clicked()
  27. {
  28. QString newPath = QFileDialog::getOpenFileName(this, trUtf8("Open SVG"),
  29. path, tr("SVG files (*.svg)"));
  30. if (newPath.isEmpty())
  31. return;
  32.  
  33. path = newPath;
  34. scene->clear();
  35.  
  36. scene->setSceneRect(SvgReader::getSizes(path));
  37.  
  38. // To set a graphic scene objects, received them with a method getElements
  39. foreach (QGraphicsRectItem *item, SvgReader::getElements(path)) {
  40. QGraphicsRectItem *rect = item;
  41. scene->addItem(rect);
  42. }
  43. }

Result

As a result, you will be able to parse the SVG file you saved to take away from him even rectangles. If you want to take all the other objects, you have pretty sweat, and write a parser for all these other objects similar to the rectangle. The only thing I want to note, do not try to use this code to open a file that was created originally in CorelDraw, the fact that the structure of the SVG file version is also very different. And this code for parsing a file that was created in the previous lesson, and the structure of the generated package CorelDraw files is slightly different and will not be read completely.

A project that brings together both lessons downloaded from the following link: SvgExample

  1. SvgReader on the Qt. Loading data from SVG file into QGraphicsScene

Video

Do you like it? Share on social networks!

F
  • Aug. 12, 2017, 6:24 p.m.

Здравствуйте!

Назрел вопрос насчет данной темы.
Реализовал отрисовку и перемещение объектов на сцене по структуре урока https://evileg.com/post/86/
Но там я в MoveItem.cpp отрисовывал не с drawRect, а с drawEllipse
void MoveItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    painter->setPen(Qt::black);
    painter->setBrush(Qt::cyan);
    //painter->drawRect(-30,-30,60,60);
    painter->drawEllipse(-30,-30,30,30);
    painter->drawEllipse(-25,-25,20,20);
    painter->drawEllipse(-20,-20,10,10);
    Q_UNUSED(option);
    Q_UNUSED(widget);
}
Далее возвращал область также прямоугольную
QRectF MoveItem::boundingRect() const
{
    return QRectF (-30,-30,30,30);
}
После добавления нескольких объектов на сцену
также проделал процесс сохранения в *.svg. При этом создался файл, где тег rect отсутствует
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg width="176.389mm" height="176.389mm"
viewBox="0 0 500 500"
xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"  version="1.2" baseProfile="tiny">
<title>SVG VKR</title>
<desc>File created by Fidan Gallyamov</desc>
<defs>
</defs>
<g fill="none" stroke="black" stroke-width="1" fill-rule="evenodd" stroke-linecap="square" stroke-linejoin="bevel" >

<g fill="none" stroke="#000000" stroke-opacity="1" stroke-width="1" stroke-linecap="square" stroke-linejoin="bevel" transform="matrix(1,0,0,1,0,0)"
font-family="MS Shell Dlg 2" font-size="8.25" font-weight="400" font-style="normal"
>
</g>

<g fill="none" stroke="#000000" stroke-opacity="1" stroke-width="1" stroke-linecap="square" stroke-linejoin="bevel" transform="matrix(1,0,0,1,416,71)"
font-family="MS Shell Dlg 2" font-size="8.25" font-weight="400" font-style="normal"
>
</g>

<g fill="#00ffff" fill-opacity="1" stroke="#000000" stroke-opacity="1" stroke-width="1" stroke-linecap="square" stroke-linejoin="bevel" transform="matrix(1,0,0,1,416,71)"
font-family="MS Shell Dlg 2" font-size="8.25" font-weight="400" font-style="normal"
>
<path vector-effect="non-scaling-stroke" fill-rule="evenodd" d="M0,-15 C0,-6.71573 -6.71573,0 -15,0 C-23.2843,0 -30,-6.71573 -30,-15 C-30,-23.2843 -23.2843,-30 -15,-30 C-6.71573,-30 0,-23.2843 0,-15 "/>
<path vector-effect="non-scaling-stroke" fill-rule="evenodd" d="M-5,-15 C-5,-9.47715 -9.47715,-5 -15,-5 C-20.5228,-5 -25,-9.47715 -25,-15 C-25,-20.5228 -20.5228,-25 -15,-25 C-9.47715,-25 -5,-20.5228 -5,-15 "/>
<path vector-effect="non-scaling-stroke" fill-rule="evenodd" d="M-10,-15 C-10,-12.2386 -12.2386,-10 -15,-10 C-17.7614,-10 -20,-12.2386 -20,-15 C-20,-17.7614 -17.7614,-20 -15,-20 C-12.2386,-20 -10,-17.7614 -10,-15 "/>
</g>

<g fill="none" stroke="#000000" stroke-opacity="1" stroke-width="1" stroke-linecap="square" stroke-linejoin="bevel" transform="matrix(1,0,0,1,416,71)"
font-family="MS Shell Dlg 2" font-size="8.25" font-weight="400" font-style="normal"
>
</g>

<g fill="none" stroke="#000000" stroke-opacity="1" stroke-width="1" stroke-linecap="square" stroke-linejoin="bevel" transform="matrix(1,0,0,1,70,190)"
font-family="MS Shell Dlg 2" font-size="8.25" font-weight="400" font-style="normal"
>
</g>

<g fill="#00ffff" fill-opacity="1" stroke="#000000" stroke-opacity="1" stroke-width="1" stroke-linecap="square" stroke-linejoin="bevel" transform="matrix(1,0,0,1,70,190)"
font-family="MS Shell Dlg 2" font-size="8.25" font-weight="400" font-style="normal"
>
<path vector-effect="non-scaling-stroke" fill-rule="evenodd" d="M0,-15 C0,-6.71573 -6.71573,0 -15,0 C-23.2843,0 -30,-6.71573 -30,-15 C-30,-23.2843 -23.2843,-30 -15,-30 C-6.71573,-30 0,-23.2843 0,-15 "/>
<path vector-effect="non-scaling-stroke" fill-rule="evenodd" d="M-5,-15 C-5,-9.47715 -9.47715,-5 -15,-5 C-20.5228,-5 -25,-9.47715 -25,-15 C-25,-20.5228 -20.5228,-25 -15,-25 C-9.47715,-25 -5,-20.5228 -5,-15 "/>
<path vector-effect="non-scaling-stroke" fill-rule="evenodd" d="M-10,-15 C-10,-12.2386 -12.2386,-10 -15,-10 C-17.7614,-10 -20,-12.2386 -20,-15 C-20,-17.7614 -17.7614,-20 -15,-20 C-12.2386,-20 -10,-17.7614 -10,-15 "/>
</g>

<g fill="none" stroke="#000000" stroke-opacity="1" stroke-width="1" stroke-linecap="square" stroke-linejoin="bevel" transform="matrix(1,0,0,1,70,190)"
font-family="MS Shell Dlg 2" font-size="8.25" font-weight="400" font-style="normal"
>
</g>

<g fill="none" stroke="#000000" stroke-opacity="1" stroke-width="1" stroke-linecap="square" stroke-linejoin="bevel" transform="matrix(1,0,0,1,319,236)"
font-family="MS Shell Dlg 2" font-size="8.25" font-weight="400" font-style="normal"
>
</g>

<g fill="#00ffff" fill-opacity="1" stroke="#000000" stroke-opacity="1" stroke-width="1" stroke-linecap="square" stroke-linejoin="bevel" transform="matrix(1,0,0,1,319,236)"
font-family="MS Shell Dlg 2" font-size="8.25" font-weight="400" font-style="normal"
>
<path vector-effect="non-scaling-stroke" fill-rule="evenodd" d="M0,-15 C0,-6.71573 -6.71573,0 -15,0 C-23.2843,0 -30,-6.71573 -30,-15 C-30,-23.2843 -23.2843,-30 -15,-30 C-6.71573,-30 0,-23.2843 0,-15 "/>
<path vector-effect="non-scaling-stroke" fill-rule="evenodd" d="M-5,-15 C-5,-9.47715 -9.47715,-5 -15,-5 C-20.5228,-5 -25,-9.47715 -25,-15 C-25,-20.5228 -20.5228,-25 -15,-25 C-9.47715,-25 -5,-20.5228 -5,-15 "/>
<path vector-effect="non-scaling-stroke" fill-rule="evenodd" d="M-10,-15 C-10,-12.2386 -12.2386,-10 -15,-10 C-17.7614,-10 -20,-12.2386 -20,-15 C-20,-17.7614 -17.7614,-20 -15,-20 C-12.2386,-20 -10,-17.7614 -10,-15 "/>
</g>

<g fill="none" stroke="#000000" stroke-opacity="1" stroke-width="1" stroke-linecap="square" stroke-linejoin="bevel" transform="matrix(1,0,0,1,319,236)"
font-family="MS Shell Dlg 2" font-size="8.25" font-weight="400" font-style="normal"
>
</g>

<g fill="none" stroke="#000000" stroke-opacity="1" stroke-width="1" stroke-linecap="square" stroke-linejoin="bevel" transform="matrix(1,0,0,1,0,0)"
font-family="MS Shell Dlg 2" font-size="8.25" font-weight="400" font-style="normal"
>
</g>
</g>
</svg>
Сразу бросается в глаза отличие
<path vector-effect="non-scaling-stroke">
У вас там просто none
 
Не подскажите как пропарсить данный файл???
Заранее спасибо)).
 
 
 
Evgenii Legotckoi
  • Aug. 13, 2017, 1:56 p.m.

Там в коде  у меня есть:

gNode.firstChildElement("rect"); 
Вам же стоит использовать просто path в данном случае.
gNode.firstChildElement("path"); 
Аттрибуты забирать аналогично, как они забираются из rect в моём коде. Делайте по аналогии, при возникновении некоторых проблем используйте qDebug() что понять, что идёт не так.
F
  • Aug. 14, 2017, 2:11 a.m.

Для формирования задал объект QGraphiscPathItem

QGraphicsPathItem *rect = new QGraphicsPathItem();
rect->setFlag(QGraphicsRectItem::ItemIsMovable);
QDomElement gElement = gNode.toElement();
rect->setPath();
у тега path есть атрибут "d". Пытаюсь считать этот атрибут с помощью setPath, который требует параметры типа QPainterPath
Как передать методу данные параметры в требуемом типе и правильно ли я сделал воспользовавшись QGraphicsPathItem?
F
  • Aug. 14, 2017, 2:22 a.m.

А кажется понял, я должен из атрибутов path сформировать заново элементы Ellipse, просто нужно правильно подобрать эти атрибуты.....


Evgenii Legotckoi
  • Aug. 15, 2017, 3:04 p.m.

Ну да. Там должны быть координаты, их нужно пропарсить и по ним построить Path, либо преобразовать их в эллипс. Вообще, кое-что кривовато переносится в SVG местами.

c
  • Oct. 15, 2017, 8:27 p.m.

How can I open another polygon type such as circle(ellipse) ?

Evgenii Legotckoi
  • Oct. 16, 2017, 12:52 a.m.

The principle will be similar. You need to research content of svg file and make parsing of needed tag.

For Example:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->

<svg
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:cc="http://creativecommons.org/ns#"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:svg="http://www.w3.org/2000/svg"
   xmlns="http://www.w3.org/2000/svg"
   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
   width="210mm"
   height="297mm"
   viewBox="0 0 744.09448819 1052.3622047"
   id="svg2"
   version="1.1"
   inkscape:version="0.91 r13725"
   sodipodi:docname="circle.svg">
  <defs
     id="defs4" />
  <sodipodi:namedview
     id="base"
     pagecolor="#ffffff"
     bordercolor="#666666"
     borderopacity="1.0"
     inkscape:pageopacity="0.0"
     inkscape:pageshadow="2"
     inkscape:zoom="0.49497475"
     inkscape:cx="405.75379"
     inkscape:cy="583.59967"
     inkscape:document-units="px"
     inkscape:current-layer="layer1"
     showgrid="false"
     inkscape:window-width="1366"
     inkscape:window-height="703"
     inkscape:window-x="0"
     inkscape:window-y="0"
     inkscape:window-maximized="1" />
  <metadata
     id="metadata7">
    <rdf:RDF>
      <cc:Work
         rdf:about="">
        <dc:format>image/svg+xml</dc:format>
        <dc:type
           rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
        <dc:title></dc:title>
      </cc:Work>
    </rdf:RDF>
  </metadata>
  <g
     inkscape:label="Layer 1"
     inkscape:groupmode="layer"
     id="layer1">
    <rect
       style="fill:#0000ff;fill-rule:evenodd;stroke:#000000;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
       id="rect3336"
       width="222.23357"
       height="131.31982"
       x="125.25892"
       y="219.99651" />
    <ellipse
       style="fill:#ff0000;fill-rule:evenodd;stroke:#000000;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
       id="path3338"
       cx="287.89346"
       cy="466.47372"
       rx="89.90358"
       ry="74.751289" />
  </g>
</svg>
In this code You can try to get ellipse.
Method for parsing will be same.
QList<QGraphicsEllipseItem*> SvgReader::getEllipses(const QString filename)
{
    QList<QGraphicsEllipseItem *> ellipsesList;

    QDomDocument doc;
    QFile file(filename);

    if (!file.open(QIODevice::ReadOnly) || !doc.setContent(&file))
        return ellipsesList;

    QDomNodeList gList = doc.elementsByTagName("g");
    for (int i = 0; i < gList.size(); i++) {
        QDomNode gNode = gList.item(i);
        QDomElement ellipseElement = gNode.firstChildElement("ellipse");

        if (ellipseElement.isNull()){
            continue;
        } else {
            QGraphicsEllipseItem *ellipseItem = new QGraphicsEllipseItem();
            ellipseItem->setFlag(QGraphicsItem::ItemIsMovable);

            QDomElement gElement = gNode.toElement();
            ellipseItem->setRect(ellipseElement.attribute("cx").toFloat() - ellipseElement.attribute("rx").toFloat(),
                                 ellipseElement.attribute("cy").toFloat() - ellipseElement.attribute("ry").toFloat(),
                                 ellipseElement.attribute("rx").toFloat() * 2,
                                 ellipseElement.attribute("ry").toFloat() * 2);

            QColor fillColor(ellipseElement.attribute("style").split(";").at(0).split(":").at(1));
            ellipseItem->setBrush(QBrush(fillColor));

            QColor strokeColor(gElement.attribute("stroke", "#000000"));
            strokeColor.setAlphaF(gElement.attribute("stroke-opacity").toFloat());

            ellipseItem->setPen(QPen(strokeColor,gElement.attribute("stroke-width", "0").toInt()));
            ellipsesList.append(ellipseItem);
        }
    }
    file.close();
    return ellipsesList;
}
c
  • Oct. 16, 2017, 7:53 p.m.

Thank You sir, So how can I change this method


QRectF SvgReader::getSizes(const QString filename)
{
    QDomDocument doc;       // initialize the stack object QDomDocument
    QFile file(filename);   // Open your SVG-file
    // If it is not opened, or have failed to transmit the contents in QDocDocument
    if (!file.open(QIODevice::ReadOnly) || !doc.setContent(&file))
        return QRectF(0,0,200,200); // the return values for the default scene
 
    /* Then pick up a list of items with the tag svg.
      * If a list of elements is not empty,
      * take the size of the graphic scenes.
     * */
    QDomNodeList list = doc.elementsByTagName("svg");
    if(list.length() > 0) {
        QDomElement svgElement = list.item(0).toElement();
        QStringList parameters = svgElement.attribute("viewBox").split(" ");
        return QRectF(parameters.at(0).toInt(),
                      parameters.at(1).toInt(),
                      parameters.at(2).toInt(),
                      parameters.at(3).toInt());
    }
    return QRectF(0,0,200,200);
}

which class I need to use instead of QRectF
Evgenii Legotckoi
  • Oct. 16, 2017, 9:25 p.m.

For what You want to use another class instead of QRectF?

This static method return just size of QGraphicsScene from svg file. QRectF should be enough.
c
  • Oct. 17, 2017, 2:09 a.m.

Sir,I tried your code for open ellipse item.but my program not open ellipse item.what should I need to do?


here is my code :

readsvg.cpp

QList<QGraphicsEllipseItem*> ReadSVG::getEllipses(const QString filename)
{
    QList<QGraphicsEllipseItem *> ellipsesList;

    QDomDocument doc;
    QFile file(filename);

    if (!file.open(QIODevice::ReadOnly) || !doc.setContent(&file))
        return ellipsesList;

    QDomNodeList gList = doc.elementsByTagName("g");
    for (int i = 0; i < gList.size(); i++) {
        QDomNode gNode = gList.item(i);
        QDomElement ellipseElement = gNode.firstChildElement("ellipse");

        if (ellipseElement.isNull()){
            continue;
        } else {
            QGraphicsEllipseItem *ellipseItem = new QGraphicsEllipseItem();
            ellipseItem->setFlag(QGraphicsItem::ItemIsMovable);

            QDomElement gElement = gNode.toElement();
            ellipseItem->setRect(ellipseElement.attribute("cx").toFloat() - ellipseElement.attribute("rx").toFloat(),
                                 ellipseElement.attribute("cy").toFloat() - ellipseElement.attribute("ry").toFloat(),
                                 ellipseElement.attribute("rx").toFloat() * 2,
                                 ellipseElement.attribute("ry").toFloat() * 2);

            QColor fillColor(ellipseElement.attribute("style").split(";").at(0).split(":").at(1));
            ellipseItem->setBrush(QBrush(fillColor));

            QColor strokeColor(gElement.attribute("stroke", "#000000"));
            strokeColor.setAlphaF(gElement.attribute("stroke-opacity").toFloat());

            ellipseItem->setPen(QPen(strokeColor,gElement.attribute("stroke-width", "0").toInt()));
            ellipsesList.append(ellipseItem);
        }
    }
    file.close();
    return ellipsesList;
}


QRectF ReadSVG::getSizes(const QString filename)
{
    QDomDocument doc;       // initialize the QDomDocument object on the stack
    QFile file(filename);   // Open our SVG file
    // If it did not open or could not transfer content to QDocDocument
    if (!file.open(QIODevice::ReadOnly) || !doc.setContent(&file))
        return QRectF(0,0,200,200); // then return the values ​​for the default scene
    QDomNodeList list = doc.elementsByTagName("svg");
    if(list.length() > 0) {
        QDomElement svgElement = list.item(0).toElement();
        QStringList parameters = svgElement.attribute("viewBox").split(" ");
        return QRectF(parameters.at(0).toInt(),
                      parameters.at(1).toInt(),
                      parameters.at(2).toInt(),
                      parameters.at(3).toInt());
    }
    return QRectF(0,0,200,200);
}
widget.cpp

void Widget::on_btnOpen_clicked()
{
    QString newPath = QFileDialog::getOpenFileName(this, trUtf8("Open SVG"),
                                                   path, tr("SVG files (*.svg)"));
    if (newPath.isEmpty())
        return;

    path = newPath;
    scene->clear();

    scene->setSceneRect(ReadSVG::getSizes(path));
    foreach (QGraphicsEllipseItem *itemE, ReadSVG::getEllipses(path)) {
        QGraphicsEllipseItem *elips = itemE;
        scene->addItem(elips);
    }


}
Evgenii Legotckoi
  • Oct. 17, 2017, 11:44 a.m.

I think You have another version of SVG file. First, need to see content of SVG file. It is simple XML-format, therefore just need to research content.

Do You want just open SVG file or make special graphics object (from QGraphicsItem etc.), which you will change it on QGraphicsScene?
If You want just open SVG file without changes, You can use QSvgWidget for it.
Otherwise, I suggest You create topic on forum of this site , where You can attach svg file, and we continue discuss about this issue.
c
  • Oct. 17, 2017, 3:29 p.m.

Sir I post is as a topic,please help me to solve this problem

Михаиллл
  • June 10, 2019, 9:43 p.m.
  • (edited)

Скажите пожалуйста, как пользоваться QSvgRenderer и загружать целиком в QGraphicsScene ?
Если делаю так, то получаю белый экран:

  1. QString newPath = QFileDialog::getOpenFileName(this, trUtf8("Open SVG"),
  2. path, tr("SVG files (*.svg)"));
  3.  
  4. if (newPath.isEmpty())
  5. return;
  6.  
  7. path = newPath;
  8. scene->clear();
  9.  
  10. QSvgRenderer w;
  11. w.load(path);
  12. scene->setSceneRect(w.viewBoxF());
  13.  

А если делаю с помощью вашего класса, то тоже получаю белый экрак.
Рисую на сцене так: Qt/C++ - Урок 021. Рисование мышью в Qt

Михаиллл
  • June 11, 2019, 12:41 p.m.

Вот так целиком добавлять можно

  1. QString newPath = QFileDialog::getOpenFileName(this, trUtf8("Open SVG"),
  2. path, tr("SVG files (*.svg)"));
  3.  
  4. if (newPath.isEmpty())
  5. return;
  6.  
  7. path = newPath;
  8. scene->clear();
  9.  
  10. //QSvgRenderer myRenderer;
  11. QSvgRenderer* myRenderer = new QSvgRenderer();
  12. myRenderer->load(path);
  13. //scene->setSceneRect(myRenderer->viewBoxF());
  14. QGraphicsSvgItem *myItem = new QGraphicsSvgItem();
  15. myItem->setSharedRenderer(myRenderer);
  16. scene->addItem(myItem);

Comments

Only authorized users can post comments.
Please, Log in or Sign up
  • Last comments
  • AK
    April 1, 2025, 11:41 a.m.
    Добрый день. В данный момент работаю над проектом, где необходимо выводить звук из программы в определенное аудиоустройство (колонки, наушники, виртуальный кабель и т.д). Пишу на Qt5.12.12 поско…
  • Evgenii Legotckoi
    March 9, 2025, 9:02 p.m.
    К сожалению, я этого подсказать не могу, поскольку у меня нет необходимости в обходе блокировок и т.д. Поэтому я и не задавался решением этой проблемы. Ну выглядит так, что вам действитель…
  • VP
    March 9, 2025, 4:14 p.m.
    Здравствуйте! Я устанавливал Qt6 из исходников а также Qt Creator по отдельности. Все компоненты, связанные с разработкой для Android, установлены. Кроме одного... Когда пытаюсь скомпилиров…
  • ИМ
    Nov. 22, 2024, 9:51 p.m.
    Добрый вечер Евгений! Я сделал себе авторизацию аналогичную вашей, все работает, кроме возврата к предидущей странице. Редеректит всегда на главную, хотя в логах сервера вижу запросы на правильн…
  • Evgenii Legotckoi
    Oct. 31, 2024, 11:37 p.m.
    Добрый день. Да, можно. Либо через такие же плагины, либо с постобработкой через python библиотеку Beautiful Soup