Results 1 to 6 of 6

Thread: Printing QwtPlot on QPrinter

  1. #1
    Join Date
    Jun 2011
    Location
    Chennai, India
    Posts
    30
    Thanks
    13
    Thanked 6 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Question Printing QwtPlot on QPrinter

    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?
    Qt Code:
    1. class HistogramPlot: public QwtPlot
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void HistogramPlot::writeToPrinter(QPrinter* printer)
    2. {
    3. QwtPlotRenderer rend;
    4. QPainter painter(printer);
    5.  
    6. rend.setDiscardFlag(QwtPlotRenderer::DiscardNone, true);
    7. rend.setDiscardFlag(QwtPlotRenderer::DiscardBackground, true);
    8.  
    9. QwtScaleMap *maps = new QwtScaleMap[this->axisCnt];
    10. for(int axis = 0; axis< this->axisCnt; axis++)
    11. maps[axis].setTransformation(xx);
    12. rend.render(this, &painter, printer->pageRect());
    13.  
    14. painter.end();
    15. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. HistogramPlot *qp = new HistogramPlot;
    2. //...(Other Code Here)
    3. QPrinter printer;
    4. printer.setOrientation(QPrinter::Portrait);
    5. printer.setOutputFormat(QPrinter::PdfFormat);
    6. printer.setOutputFileName("temp.pdf");
    7. printer.setPageSize(QPrinter::A4);
    8. qp->writeToPrinter(&printer);
    To copy to clipboard, switch view to plain text mode 

    I use Qt 4.7.1 and Qwt 6.0.0

  2. #2
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,311
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Printing QwtPlot on QPrinter

    From the tvplot example in SVN trunk:

    Qt Code:
    1. QwtPlotRenderer renderer;
    2. renderer.renderDocument(plot, "tvplot.pdf", QSizeF(300, 200), 85);
    To copy to clipboard, switch view to plain text mode 

    Uwe

  3. #3
    Join Date
    Jun 2011
    Location
    Chennai, India
    Posts
    30
    Thanks
    13
    Thanked 6 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Printing QwtPlot on QPrinter

    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
    Qt Code:
    1. HistogramPlot *qp = new HistogramPlot;
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. int nValues = model->rowCount();
    2. for(int v=0; v<nValues; v++)
    3. {
    4. double *values = new double[nValues];
    5. for(int x=0; x<nValues; x++)
    6. values[x] = 0;
    7. values[v] = model->data(model->index(v, 1)).toDouble();
    8. Histogram *h = new Histogram(model->data(model->index(v, 0)).toString(),
    9. QColor(model->data(model->index(v,0), Qt::DecorationRole).toString()));
    10. h->setValues(nValues, values);
    11. h->attach(qp);
    12. }
    13.  
    14. QwtLegend *legend = new QwtLegend;
    15. qp->insertLegend(legend, QwtPlot::RightLegend);
    16. fl->addWidget(qp, 1);
    To copy to clipboard, switch view to plain text mode 
    Last edited by gkarthick5; 2nd June 2011 at 08:43.

  4. #4
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,311
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Printing QwtPlot on QPrinter

    Post a small compilable demo application and I will have a look at it.

    Uwe

  5. #5
    Join Date
    Jun 2011
    Location
    Chennai, India
    Posts
    30
    Thanks
    13
    Thanked 6 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Printing QwtPlot on QPrinter

    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.

  6. #6
    Join Date
    Jun 2011
    Location
    Chennai, India
    Posts
    30
    Thanks
    13
    Thanked 6 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Printing QwtPlot on QPrinter

    I realized the problem now. I was doing the drawing in the drawCanvas() method of my plot, while the renderDocument() actually calls drawItems().

Similar Threads

  1. Replies: 13
    Last Post: 18th December 2009, 09:43
  2. printing with QwtPlot::print
    By user in forum Qwt
    Replies: 1
    Last Post: 7th March 2008, 07:56
  3. Safe checking the QPrinter::PrintState after printing
    By mSergey in forum Qt Programming
    Replies: 1
    Last Post: 18th January 2008, 16:37
  4. Printing via QPrinter doesnt work
    By mSergey in forum Qt Programming
    Replies: 1
    Last Post: 16th January 2008, 19:00
  5. QPrinter::PrinterMode and QPrinter::setResolution??
    By SkripT in forum Qt Programming
    Replies: 2
    Last Post: 28th April 2006, 11:59

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.