PDA

View Full Version : Printing QwtPlot on QPrinter



gkarthick5
1st June 2011, 14:52
I have a QwtPlot in which i add multiple Histograms (Histogram is the class from the "tvplot" example). When i try to print the plot to a QPrinter ( i tried with a pdf and jpeg file), i get the axes, labels, title and legend printed. But no data. What should i do to get the bars also printed?

class HistogramPlot: public QwtPlot


void HistogramPlot::writeToPrinter(QPrinter* printer)
{
QwtPlotRenderer rend;
QPainter painter(printer);

rend.setDiscardFlag(QwtPlotRenderer::DiscardNone, true);
rend.setDiscardFlag(QwtPlotRenderer::DiscardBackgr ound, true);

QwtScaleMap *maps = new QwtScaleMap[this->axisCnt];
QwtScaleTransformation* xx = new QwtScaleTransformation(QwtScaleTransformation::Lin ear);
for(int axis = 0; axis< this->axisCnt; axis++)
maps[axis].setTransformation(xx);
rend.render(this, &painter, printer->pageRect());

painter.end();
}


HistogramPlot *qp = new HistogramPlot;
//...(Other Code Here)
QPrinter printer;
printer.setOrientation(QPrinter::Portrait);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName("temp.pdf");
printer.setPageSize(QPrinter::A4);
qp->writeToPrinter(&printer);

I use Qt 4.7.1 and Qwt 6.0.0

Uwe
2nd June 2011, 07:44
From the tvplot example in SVN trunk:


QwtPlotRenderer renderer;
renderer.renderDocument(plot, "tvplot.pdf", QSizeF(300, 200), 85);

Uwe

gkarthick5
2nd June 2011, 08:08
Thanks for the Reply. I tried that also. But getting the same result (everything other than the data plots are getting printed). Is there some problem when multiple histograms are enabled in the plot(i have around 16 of them). Also, is there any specific order in which the attaching or any other thing like that have to be done?
This is how i add the histograms to the plot

HistogramPlot *qp = new HistogramPlot;

int nValues = model->rowCount();
for(int v=0; v<nValues; v++)
{
double *values = new double[nValues];
for(int x=0; x<nValues; x++)
values[x] = 0;
values[v] = model->data(model->index(v, 1)).toDouble();
Histogram *h = new Histogram(model->data(model->index(v, 0)).toString(),
QColor(model->data(model->index(v,0), Qt::DecorationRole).toString()));
h->setValues(nValues, values);
h->attach(qp);
}

QwtLegend *legend = new QwtLegend;
qp->insertLegend(legend, QwtPlot::RightLegend);
fl->addWidget(qp, 1);

Uwe
2nd June 2011, 09:46
Post a small compilable demo application and I will have a look at it.

Uwe

gkarthick5
7th June 2011, 06:56
Thanks, I was setting the transformation wrongly to use the plot's width and height instead of the number of bars and maxValue of the bars. Now its working.

gkarthick5
28th June 2011, 12:33
I realized the problem now. I was doing the drawing in the drawCanvas() method of my plot, while the renderDocument() actually calls drawItems().