QCustomPlot Discussion and Comments

Painting plot to PDF produces blurry image and textReturn to overview

I am trying to export a plot to a PDF and while the result is readable, it is very blurry. I've tried reducing the scale but I just end up with a smaller blurry image. Using HighResolution mode just gets me a small grey square instead of the desired image. Can anyone spot what I might be doing wrong?

int plotW = 451;
    int plotH = 315;
    QCustomPlot newPlot(this);

    newPlot.setBaseSize(plotW, plotH);
    newPlot.setMinimumSize(plotW, plotH);
    newPlot.addGraph();
    newPlot.graph(0)->setPen(QPen(Qt::blue));
    newPlot.addGraph();
    newPlot.graph(1)->setPen(QPen(Qt::red));
    newPlot.addGraph();
    newPlot.graph(2)->setPen(QPen(Qt::green));
    newPlot.addGraph();
    newPlot.graph(3)->setPen(QPen(Qt::cyan));
    // give the axes some labels:
    newPlot.xAxis->setLabel(tr("Time Elapsed (s)"));
    // set axes ranges, so we see all data:
    newPlot.xAxis->setRange(0, 735);
    newPlot.yAxis->setLabel(tr("Delta % Transmission"));
    newPlot.yAxis->setRange(-0.15, 1.10);
    newPlot.xAxis->setRange(ui->plotwidget->xAxis->range());
    newPlot.graph(0)->setData(ui->plotwidget->graph(0)->data(), true);
    newPlot.graph(1)->setData(ui->plotwidget->graph(1)->data(), true);
    newPlot.graph(2)->setData(ui->plotwidget->graph(2)->data(), true);
    newPlot.graph(3)->setData(ui->plotwidget->graph(3)->data(), true);
    QFont courier = QApplication::font();
    courier.setFamily("DejaVu Sans Mono");
    courier.setPointSize(12);
    courier.setStyleStrategy(QFont::PreferQuality););
    newPlot.setFont(courier);

    QString defaultPrinter = PrintManager::instance()->getDefaultPrinter();

    QPaintDevice* paintDevice = NULL;

    QPrinter printer;
    printer.setOutputFileName(GRAPH_TEMP_PDF);
    printer.setOutputFormat(QPrinter::PdfFormat);
    printer.setPaperSize(QPrinter::A4);
    printer.setColorMode(QPrinter::Color);
    paintDevice = &printer;

    if(defaultPrinter.contains("zerba", Qt::CaseInsensitive))
    {
        QImage image(newPlot.width(), newPlot.height(), QImage::Format_RGB32);
        paintDevice = ℑ
    }

    QCPPainter painter;
    if(painter.begin(paintDevice))
    {
        double xscale = printer.pageRect().width()/double(newPlot.width());
        double yscale = printer.pageRect().height()/double(newPlot.height());
        double scale = qMin(xscale, yscale);
        painter.translate(printer.paperRect().x() + printer.pageRect().width()/2,
                           printer.paperRect().y() + printer.pageRect().height()/2);
        painter.scale(scale, scale);
        painter.translate(-width()/2, -height()/2);
        painter.setRenderHint(QPainter::Antialiasing, true);
        painter.setRenderHint(QPainter::TextAntialiasing, true);
        painter.setAntialiasing(true);
        painter.setMode(QCPPainter::pmVectorized);
        painter.setMode(QCPPainter::pmNoCaching);
        painter.setMode(QCPPainter::pmNonCosmetic);
        QFont painterFont = QApplication::font();
        painterFont.setPointSize(18);
        painter.setFont(painterFont);

        newPlot.render(&painter);
        painter.end();
    }

Hi!
Do you observe the same phenomenon when using QCustomPlot's savePdf method? If not, you could look at its code for orientation. Also have a look at the QCP Document Integration use case tutorial.

If it also shows the blurry text, make sure it's not an issue with your PDF-Viewer. e.g. open the pdf in a vector editor (inkscape, adobe illustrator, etc.) and check that the export is vectorized and not rastered.
Also note that Qt does not embed fonts. So if you're using a font that is not available in the system wide font database, the replacement font might look unexpected in the PDF-Viewer.

I noticed you're using QWidget::render to actually draw the plot. I recommend not doing that and rather trying QCustomPlot::toPainter which is intended for that purpose.