A few questions about pdf file generation using render & QPdfWriter & QCustomplot
-
Qt Version :5.8
OS : WindowsMy Question:
1、
I want to export the entire widget to pdf,I find the render method,So here's the codeQFile file("D:/test.pdf"); file.open(QIODevice::Append); QPdfWriter* pdf= new QPdfWriter(&file); pdf->setPageSize(QPagedPaintDevice::A4); pdf->setResolution(QPrinter::ScreenResolution); QPainter printpainter; printpainter.begin(pdf); double scale = (double)printer->width() / m_wgtRect.width(); printpainter.scale(scale, scale); m_wgtRect->render(&printpainter); printpainter.end()
But there are images and qcustomplot in the widget,After they go through render, they will not be clear when zoomed in,So how do I handle images and qcustomplot in render?
2、
Notice the savePdf method in qcustomplot,Look at the source code and found,What he was using was QPrinter & QPainter,I replaced the QPrinter with a QPdfWriter,The text ratio in the generated pdf file is wrong and becomes very large, especially axis labelText,Further trace code I find the difference:QPrinter* printer = new QPrinter(QPrinter::ScreenResolution); printer->setOutputFormat(QPrinter::PdfFormat); printer->setPageSize(QPagedPaintDevice::A4); printer->setOutputFileName("D://test2.pdf"); QPainter painter2(printer); auto rt2 = painter2.fontMetrics().boundingRect(0, 0, 0, 0, Qt::TextDontClip, "x"); painter2.end();
and
QPdfWriter* pdf = new QPdfWriter("D://test.pdf"); pdf->setResolution(QPrinter::ScreenResolution); pdf->setPageSize(QPagedPaintDevice::A4); QPainter painter1(pdf); auto rt1 = painter1.fontMetrics().boundingRect(0, 0, 0, 0, Qt::TextDontClip, "x"); painter1.end();
Just use a different device to initialize QPainter,The rect of the resulting font is different:
Use QPrinter : (0,0,6,12)
Use QPdfWriter: (0,0,75,150)
Even if the setFont method is used to modify the font of QPdfWriter, it will not work,So, how can QPdfWriter get the same font rect as QPrinter?Best wishes