PDA

View Full Version : How to get customized background printed?



ryvius
9th February 2009, 10:49
Hello,

I want to customize the background of a plot to make the output clearer. To do this I painted polygons onto the background canvas. The problem with this is, that using the print() method only outputs the originally chosen background color and ignores the polygons drawn onto the canvas.

Grabbing the whole widget and printing its pixmap works like intended and returns the complete background, see graph3.png

But when I try to save it as a pdf the polygons are left out, see graph.pdf

Is there a way to get my polygons into the pdf output or do I have to live with jpg or png?

Thanks for your help.

Code used for plotting:


void CGraph::printGraph()
{
this->replot();
QwtPlotPrintFilter filter;
//filter.setOptions(QwtPlotPrintFilter::PrintAll | QwtPlotPrintFilter::Options::PrintBackground);

int options = QwtPlotPrintFilter::PrintAll;
//options |= QwtPlotPrintFilter::PrintBackground;
//options |= QwtPlotPrintFilter::PrintFrameWithScales;
filter.setOptions(options);

QPrinter printer;
printer.setOrientation(QPrinter::Landscape);
printer.setOutputFileName("graph.pdf");
print(printer, filter);

QPixmap *image;
QwtPlotCanvas *canvas;
this->canvas();
canvas = this->canvas();
image = canvas->paintCache();
image->save("graph2.png");

QPixmap pixmap= QPixmap::grabWidget(this);
pixmap.save("graph3.png", "png" );
}

Code for painting the background:


void CGraph::drawCanvas(QPainter* painter)
{
QwtPainter qwtPainter;
QwtPolygon polygon(3);

painter->setPen(Qt::NoPen);
painter->setBrush(Qt::red);
painter->translate(0,canvas()->height());

polygon.setPoint(0,0,0);
polygon.setPoint(1,canvas()->width(),0);
polygon.setPoint(2,canvas()->width(),-canvas()->height());

painter->setBrush(QBrush(QColor(255,100,0)));
qwtPainter.drawPolygon(painter,polygon);

polygon.setPoint(2,canvas()->width(),-(double)canvas()->height()*6/7);
qwtPainter.drawPolygon(painter,polygon);

painter->setBrush(QBrush(QColor(255,200,0)));
polygon.setPoint(2,canvas()->width(),-(double)canvas()->height()*5/7);
qwtPainter.drawPolygon(painter,polygon);

painter->setBrush(Qt::yellow);
polygon.setPoint(2,canvas()->width(),-(double)canvas()->height()*4/7);
qwtPainter.drawPolygon(painter,polygon);

painter->setBrush(QBrush(QColor(180,230,0)));
polygon.setPoint(2,canvas()->width(),-(double)canvas()->height()*3/7);
qwtPainter.drawPolygon(painter,polygon);

painter->setBrush(Qt::darkGreen);
polygon.setPoint(2,canvas()->width(),-(double)canvas()->height()*2/7);
qwtPainter.drawPolygon(painter,polygon);

painter->setBrush(Qt::green);
polygon.setPoint(2,canvas()->width(),-(double)canvas()->height()*1/7);
qwtPainter.drawPolygon(painter,polygon);
}

Uwe
10th February 2009, 07:15
You also need to overload your QwtPlot::printCanvas.

But I would implement your background as a plot item. Look at the cpuplot example: in cpuplot.cpp, where you find such a Background plot item.

Uwe

ryvius
10th February 2009, 13:12
Danke Uwe,

the CpuPlot example was what I was looking for. After using an overloaded QwtPlotItem and some tweaking and translating of my coordinates printing worked. :)