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);
}
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);
}