2017-10-15 121 views
0

我试图创建并保存QGraphicsScene.svg文件,我的代码正常工作。其中创建矩形,圈并将其保存到.svg但问题是哪些只能打开矩形,我怎样才能实现这个打开圆(椭圆太)请帮助我。如何使用QT C++打开SVG

这是我的代码。

readsvg.h

#ifndef READSVG_H 
#define READSVG_H 
#include <QList> 
#include <QGraphicsRectItem> 

class ReadSVG 
{ 
public: 
    ReadSVG(); 
    static QList<QGraphicsRectItem *> getElements(const QString filename); 
    static QRectF getSizes(const QString filename); 

    //static QList<QGraphicsEllipseItem *> getElements(const QString filename); 
    //static QRectF getSizes(const QString filename); 

}; 

#endif // READSVG_H 

readsvg.cpp

#include "readsvg.h" 
#include <QPen> 
#include <QFile> 
#include <QMessageBox> 
#include <QDomDocument> 
#include <QStringList> 

ReadSVG::ReadSVG() 
{ 

} 

QList<QGraphicsRectItem *> ReadSVG::getElements(const QString filename) 
{ 
    QList<QGraphicsRectItem *> rectList; // We declare in the stack a list of rectangles 

    QDomDocument doc;  // document object 
    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 rectList; // then return the list, but empty 

    // Look in the document for all objects with the tag g 
    QDomNodeList gList = doc.elementsByTagName("g"); 
    // We start to sort them out 
    for (int i = 0; i < gList.size(); i++) { 
     QDomNode gNode = gList.item(i);  // Select the node from the list 
     QDomElement rectangle = gNode.firstChildElement("rect"); // And we search in it for an element with the tag rect 

     // If the resulting elements are not zero, then 
     if (rectangle.isNull()){ 
      continue; 
     } else { 
      // begin to form a rectangle 
      QGraphicsRectItem *rect = new QGraphicsRectItem(); 
      // This flag makes the object moveable, it will be required for verification 
      rect->setFlag(QGraphicsItem::ItemIsMovable); 
      // We take sizes from the rect tag 
      QDomElement gElement = gNode.toElement(); 
      rect->setRect(rectangle.attribute("x").toInt(), 
          rectangle.attribute("y").toInt(), 
          rectangle.attribute("width").toInt(), 
          rectangle.attribute("height").toInt()); 

      /* 
       We take the parameters of the colors gNode from the node element 
       yes yes yes ... it's from gNode, not from rectangle. These parameters are stored in the tag g 
      * */ 
      QColor fillColor(gElement.attribute("fill", "#ffffff")); // fill color 
      fillColor.setAlphaF(gElement.attribute("fill-opacity","0").toFloat()); 
      rect->setBrush(QBrush(fillColor)); 

      // as well as the color and thickness of the outline 
      QColor strokeColor(gElement.attribute("stroke", "#000000")); 
      strokeColor.setAlphaF(gElement.attribute("stroke-opacity").toFloat()); 

      rect->setPen(QPen(strokeColor,gElement.attribute("stroke-width", "0").toInt())); 
      rectList.append(rect); // add a rectangle to the list 
     } 
    } 
    file.close(); 
    return rectList; 
} 

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 

    /* Next, we take the list of elements with the tag svg. 
    * In case the list of elements is not empty, 
    * then we will take the dimensions of the graphic 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.h

#ifndef WIDGET_H 
#define WIDGET_H 

#include <QGraphicsScene> 
#include <QGraphicsRectItem> 
#include <QWidget> 
#include <QtCore> 
#include <QtGui> 
#include <QSvgGenerator> 

namespace Ui { 
class Widget; 
} 

class Widget : public QWidget 
{ 
    Q_OBJECT 

public: 
    explicit Widget(QWidget *parent = 0); 
    ~Widget(); 

private slots: 
    void on_pushButton_clicked(); 

    void on_btnSave_clicked(); 

    void on_btnOpen_clicked(); 

private: 
    Ui::Widget *ui; 
    QGraphicsScene *scene; 
    QGraphicsEllipseItem *elipse; 
    QGraphicsRectItem *rect; 
    QString path; 

}; 

#endif // WIDGET_H 

widget.cpp

#include "widget.h" 
#include "ui_widget.h" 
#include "readsvg.h" 

#include <QCursor> 
#include <QFileDialog> 

Widget::Widget(QWidget *parent) : 
    QWidget(parent), 
    ui(new Ui::Widget) 
{ 
    ui->setupUi(this); 
    scene = new QGraphicsScene(this); 
    ui->graphicsView->setScene(scene); 

    QBrush redBrush(Qt::red); 
    QBrush blueBrush(Qt::blue); 
    QPen blackPen(Qt::black); 
    blackPen.setWidth(6); 

    elipse = scene->addEllipse(10,10,100,100,blackPen,redBrush); 
    rect = scene->addRect(-10,-10,100,100,blackPen,blueBrush); 
    rect->setFlag(QGraphicsItem::ItemIsMovable, true); 


} 

Widget::~Widget() 
{ 
    delete ui; 
} 

void Widget::on_pushButton_clicked() 
{ 
    QString fileName= QFileDialog::getSaveFileName(this, "Save image", QCoreApplication::applicationDirPath(), "BMP Files (*.bmp);;JPEG (*.JPEG);;PNG (*.png)"); 
     if (!fileName.isNull()) 
     { 
      QPixmap pixMap = this->ui->graphicsView->grab(); 
      pixMap.save(fileName); 
     } 
} 

void Widget::on_btnSave_clicked() 
{ 
    // Заберём путь к файлу и его имененем, который будем создавать 
    QString newPath = QFileDialog::getSaveFileName(this, trUtf8("Save SVG"), 
     path, tr("SVG files (*.svg)")); 

    if (newPath.isEmpty()) 
     return; 

    path = newPath; 

    QSvgGenerator generator;   
    generator.setFileName(path);  
    generator.setSize(QSize(scene->width(), scene->height())); 
    generator.setViewBox(QRect(0, 0, scene->width(), scene->height())); 
    generator.setTitle(trUtf8("SVG Example"));       
    generator.setDescription(trUtf8("File created by SVG Example"));  


    QPainter painter; 
    painter.begin(&generator); 
    scene->render(&painter); 
    painter.end();    

} 


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)); // Set the size of the graphic scene 

    // Install the objects on the graphical scene, get them using the getElements 
    foreach (QGraphicsRectItem *item, ReadSVG::getElements(path)) { 
     QGraphicsRectItem *rect = item; 
     scene->addItem(rect); 
    } 
} 

回答

0

您可以用您已启动的办法继续。使用所需的标签名称查找元素。从“圈子”标签开始。这是一个卷曲标签可能看起来像什么;

<circle cx="5" cy="5" r="3" fill="#0f0"/> 
+0

我不清楚你的答案。请给我提供完整的代码来做到这一点,因为我是QT.please的新手帮助我 – Rooter