PDA

View Full Version : Insert SVG inside QTextDocument



giusepped
17th April 2009, 07:45
Sorry to re-post the same problem.
I have a QTextDocument which I fill with some tables and data.
I created the QwtPlot of these data and I would like too insert the plot inside the QTextDocument. Eventually, I would print the pdf version of the document.
I choose SVG because it can render with high and beutiful resolution.
But, how can insert the svg graphics inside the QTextDocument and then print it on QPrinter?
regards

wysota
17th April 2009, 10:53
Can QwtPlot render its vector based representation to SVG or only its bitmap SVG representation? Because if the latter, using SVG will have no positive influence on the result. I don't think there is any way to place an SVG file to a text document but if you have the size of the document, you can render the svg of the required size to png and insert the image into the document.

giusepped
20th April 2009, 07:18
Qwtplot can render on any paintdevice.
For example, when I save my plot on disk, I use print(svggenerator,filter), where svggenerator is a QSvgGenerator.
I can use png, but in that case the resolution will be not so high.
G

giusepped
20th April 2009, 08:20
Now I am trying this:


m_document->print(&printer);

QwtPlotPrintFilter filter;

int options = QwtPlotPrintFilter::PrintAll;
options &= ~QwtPlotPrintFilter::PrintBackground;
options |= QwtPlotPrintFilter::PrintFrameWithScales;
filter.setOptions(options);
printer.newPage();
solarDiagram->print(printer,filter);

where solarDiagram is the pointer to a QwtPlot.
But as you can imagine, the print contains only the plot and not the textdocument.
Thereis a way to tell QTextDocument to "paint" on printer?
Or, how can I bind two printing on the same QPrinter?

giusepped
20th April 2009, 09:25
Now I am trying this:




m_document->print(&printer);

QwtPlotPrintFilter filter;

int options = QwtPlotPrintFilter::PrintAll;
options &= ~QwtPlotPrintFilter::PrintBackground;
options |= QwtPlotPrintFilter::PrintFrameWithScales;
filter.setOptions(options);
printer.newPage();
solarDiagram->print(printer,filter);

where solarDiagram is the pointer to a QwtPlot.
But as you can imagine, the print contains only the plot and not the textdocument.
Thereis a way to tell QTextDocument to "paint" on printer?
Or, how can I bind two printing on the same QPrinter?

I tried the followind code



QPrinter printer(QPrinter::HighResolution);
m_document->setPageSize(printer.pageRect().size());


printer.setOutputFileName(fileName);
....

QPainter p;
p.begin(&printer);
m_document->drawContents(&p,printer.paperRect());
printer.newPage();
solarDiagram->print(&p,printer.paperRect(),filter);
p.end();

but the textdocument is too small to be seen in the pdf printout, and the plot is printed in the correct size and with high resolution (pdf).
Why the textdocument is not rendered in the correct size?

giusepped
21st April 2009, 04:42
I found this solution




QPrinter printer(QPrinter::HighResolution);
...
QPainter p;
int pw = printer.pageRect().width();
int ph =printer.pageRect().height();

p.begin(&printer);

p.save();
m_document->adjustSize();
QTransform tra;
tra.scale(11,11);
p.setTransform(tra);

m_document->drawContents(&p,printer.paperRect());


p.restore();
QRect rec(-10,0,pw-10,0.4*ph);
p.translate(0,0.65*ph);
solarDiagram->print(&p,rec,filter);
p.end();


This is good for A4 paper. The plot is rendered as a vector graphic and I like it.
But I would scale the painter automatically. The value "11" is taken from a trial-and-correct approach....:confused:

wysota
23rd April 2009, 19:57
Compare the size of the painter to the size of the device or use QPainter::setWindow() and QPainter::setViewport()