This is (3 steps - tricky) way, but gives you 100% accuracy of points drawn by QPainter.
Step 1:
Get some points, and create SVG file.
/// Any QPainter element. e.g QLine/QPolygon etc. fill or not any mix of QPainter shapes.
polygonPath.moveTo(10.0, 80.0);
polygonPath.lineTo(20.0, 10.0);
polygonPath.lineTo(80.0, 30.0);
polygonPath.lineTo(90.0, 70.0);
polygonPath.closeSubpath();
/// To write an SVG file, you first need to configure the output by setting the fileName or outputDevice properties.
QSvgGenerator generator;
string directory = "/home/mydir/";
generator.
setFileName(QString::fromStdString(directory
+ "test.svg"));
/// The resolution is specified in dots per inch, and is used to calculate the physical size of an SVG drawing.
generator.setResolution(90); /// If 90 - coordinates in SVG will be in scale 1:1 for size set in setSize()
generator.
setSize(QSize(1024,
768));
generator.setTitle(tr("SVG test"));
generator.setDescription(tr("QPainterPath test"));
/// Painting in QSvgGenerator is performed in the same way as for any other paint device.
/// However, it is necessary to use the QPainter::begin() and end() to explicitly begin and end painting on the device.
painter.begin(&generator);
painter.drawPath(polygonPath);
painter.end();
/// At this point SVG file was created.
/// Any QPainter element. e.g QLine/QPolygon etc. fill or not any mix of QPainter shapes.
QPainterPath polygonPath;
polygonPath.moveTo(10.0, 80.0);
polygonPath.lineTo(20.0, 10.0);
polygonPath.lineTo(80.0, 30.0);
polygonPath.lineTo(90.0, 70.0);
polygonPath.closeSubpath();
/// To write an SVG file, you first need to configure the output by setting the fileName or outputDevice properties.
QSvgGenerator generator;
string directory = "/home/mydir/";
generator.setFileName(QString::fromStdString(directory + "test.svg"));
/// The resolution is specified in dots per inch, and is used to calculate the physical size of an SVG drawing.
generator.setResolution(90); /// If 90 - coordinates in SVG will be in scale 1:1 for size set in setSize()
generator.setSize(QSize(1024, 768));
generator.setTitle(tr("SVG test"));
generator.setDescription(tr("QPainterPath test"));
/// Painting in QSvgGenerator is performed in the same way as for any other paint device.
/// However, it is necessary to use the QPainter::begin() and end() to explicitly begin and end painting on the device.
QPainter painter;
painter.begin(&generator);
painter.drawPath(polygonPath);
painter.end();
/// At this point SVG file was created.
To copy to clipboard, switch view to plain text mode
Step 2
Use QtXML to open the SVG file as XML and manipulate it using the DOM or stream functions as appropriate.
In this example the content of test.svg is:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg width="288.996mm" height="216.747mm"
xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.2" baseProfile="tiny">
<title>SVG test</title>
<desc>QPainterPath test</desc>
<defs>
</defs>
<g fill="none" stroke="black" stroke-width="1" fill-rule="evenodd" stroke-linecap="square" stroke-linejoin="bevel" >
<path vector-effect="none" fill-rule="evenodd" d="M10,80 L20,10 L80,30 L90,70 L10,80"/>
</g>
</svg>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg width="288.996mm" height="216.747mm"
xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.2" baseProfile="tiny">
<title>SVG test</title>
<desc>QPainterPath test</desc>
<defs>
</defs>
<g fill="none" stroke="black" stroke-width="1" fill-rule="evenodd" stroke-linecap="square" stroke-linejoin="bevel" >
<path vector-effect="none" fill-rule="evenodd" d="M10,80 L20,10 L80,30 L90,70 L10,80"/>
</g>
</svg>
To copy to clipboard, switch view to plain text mode
Step 3
You need to parse this:
d="M10,80 L20,10 L80,30 L90,70 L10,80"
d="M10,80 L20,10 L80,30 L90,70 L10,80"
To copy to clipboard, switch view to plain text mode
Use for example QRegex to remove all unwanted letters M,L,C...etc and pair elements to x,y coordinates.
Point1(10,80)
Point2(20,10)
Point3(80,30)
Point4(90,70)
Point5(10,80)
And you will find what we expected.
Bookmarks