Qt-jambi-interest Archive, December 2007
SVG/PDF exporting from QGraphicsView
Message 1 in thread
Is it possible to get an SVG/PDF out from a QGraphicsView?
Matias
Message 2 in thread
On Thursday 06 December 2007 4:04:46 am Matias Piipari wrote:
> Is it possible to get an SVG/PDF out from a QGraphicsView?
>
> Matias
i'd be interested in this also.
Message 3 in thread
Andrew Mason wrote:
> On Thursday 06 December 2007 4:04:46 am Matias Piipari wrote:
>> Is it possible to get an SVG/PDF out from a QGraphicsView?
>>
>> Matias
>
> i'd be interested in this also.
Hi Andrew and Matias,
This is quite possible using the render() function in QGraphicsScene. It
takes a QPainter that can be opened on any device, be it printer or SVG
generator or pixmap. By default the function will adapt the source and
target rects to render to scale the scene to fit the device it paints
on, but you can specify different rectangles if you like.
* To print to PDF you need to construct a QPrinter, set its output
format to PDF and specify a filename.
* Generating SVG's is unfortunatly not possible with the currently
released package, because it lacks a class. This is a bug and we'll try
to update shortly. What you would have done, had the class been in the
package, is: Construct a QSvgGenerator, set its filename and open a
QPainter on it and pass it to the render function.
I supplied an example that illustrates how this works.
-
PS! It is rather easy to add support for QSvgGenerator if you build from
source. Simply add the line:
<object-type name="QSvgGenerator" />
to typesystem_svg.txt in the QTJAMBI/generator directory and rebuild the
source package.
-
Gunnar
import com.trolltech.qt.core.*;
import com.trolltech.qt.core.*;
import com.trolltech.qt.gui.*;
// import com.trolltech.qt.svg.*;
public class Output {
public static void main(String args[]) {
QApplication.initialize(args);
// Populate the scene...
QGraphicsScene scene = new QGraphicsScene();
int rad = 100;
QRadialGradient gr = new QRadialGradient(0, 0, rad, - rad / 2, -rad / 2);
gr.setColorAt(0.0, new QColor(255, 255, 255, 191));
gr.setColorAt(0.2, new QColor(255, 255, 127, 191));
gr.setColorAt(0.9, new QColor(150, 150, 200, 63));
gr.setColorAt(0.95, new QColor(0, 0, 0, 127));
gr.setColorAt(1, new QColor(0, 0, 0, 0));
scene.addEllipse(new QRectF(-rad, -rad, rad * 2, rad * 2), QPen.NoPen, new QBrush(gr));
QFile f = new QFile("Output.java");
f.open(QFile.OpenModeFlag.ReadOnly);
String s = f.readAll().toString();
scene.addText(s, new QFont("Courier"));
f.dispose();
// Dump to PDF
QPrinter printer = new QPrinter();
printer.setOutputFormat(QPrinter.OutputFormat.PdfFormat);
printer.setOutputFileName("out.pdf");
QPainter pdfPainter = new QPainter(printer);
scene.render(pdfPainter);
pdfPainter.end();
// // Dump to SVG
// QSvgGenerator gen = new QSvgGenerator();
// gen.setFileName("out.svg");
// QPainter svgPainter = new QPainter(gen);
// scene.render(svgPainter);
// svgPainter.end();
}
}
Message 4 in thread
Excellent, thanks for that. Am very impressed, I have to say. It was a
10-minute job to add PDF exporting for my case where I have multiple
QGraphicsScenes to draw to different positions in the document!
On Dec 6, 2007 9:09 AM, Gunnar Sletta <gunnar@xxxxxxxxxxxxx> wrote:
> Andrew Mason wrote:
> > On Thursday 06 December 2007 4:04:46 am Matias Piipari wrote:
> >> Is it possible to get an SVG/PDF out from a QGraphicsView?
> >>
> >> Matias
> >
> > i'd be interested in this also.
>
> Hi Andrew and Matias,
>
> This is quite possible using the render() function in QGraphicsScene. It
> takes a QPainter that can be opened on any device, be it printer or SVG
> generator or pixmap. By default the function will adapt the source and
> target rects to render to scale the scene to fit the device it paints
> on, but you can specify different rectangles if you like.
>
> * To print to PDF you need to construct a QPrinter, set its output
> format to PDF and specify a filename.
>
> * Generating SVG's is unfortunatly not possible with the currently
> released package, because it lacks a class. This is a bug and we'll try
> to update shortly. What you would have done, had the class been in the
> package, is: Construct a QSvgGenerator, set its filename and open a
> QPainter on it and pass it to the render function.
>
> I supplied an example that illustrates how this works.
>
> -
>
> PS! It is rather easy to add support for QSvgGenerator if you build from
> source. Simply add the line:
>
> <object-type name="QSvgGenerator" />
>
> to typesystem_svg.txt in the QTJAMBI/generator directory and rebuild the
> source package.
>
> -
> Gunnar
>