QTextDocument :: drawContents only displays 96 dpi

I am creating a high resolution PDF document (1200 dpi) using QPrinter and QPainter. I am trying to make text in the same resolution using QTextDocument :: drawContents. The reason I want to use QTextDocument is because I need to include many tables and formatted text in the document.

My problem is that QTextDocument :: drawContents always inserts text with a screen resolution of 96 dpi in my case. All the solutions that I have found so far offer text scaling to achieve the correct size. However, this results in poor quality text that I cannot afford.

My question is: is there a way to draw high resolution QTextDocument content?

The following code creates a PDF file with 2 lines of text, one of which is drawn using QPainter :: drawText, and one is drawn using QTextDocument :: drawContents. I used the Arial 8pt font to emphasize the poor quality problem resulting from scaling.

// Read the screen resolution for scaling QPrinter screenPrinter(QPrinter::ScreenResolution); int screenResolution = screenPrinter.resolution(); // Setup the font QFont font; font.setFamily("Arial"); font.setPointSize(8); // Define locations to insert text QPoint textLocation1(20,10); QPoint textLocation2(20,20); // Define printer properties QPrinter printer(QPrinter::HighResolution); printer.setOrientation(QPrinter::Portrait); printer.setPaperSize(QPrinter::A4); printer.setResolution(1200); printer.setOutputFormat(QPrinter::PdfFormat); printer.setOutputFileName("test.pdf"); // Write text using QPainter::drawText QPainter painter; painter.begin(&printer); painter.setFont(font); painter.drawText(textLocation1, "QPainter::drawText"); // Write text using QTextDocument::drawContents QTextDocument doc; doc.setPageSize(printer.pageRect().size()); QTextCursor cursor(&doc); QTextCharFormat charFormat; charFormat.setFont(font); cursor.insertText("QTextDocument::drawContents", charFormat); painter.save(); painter.translate(textLocation2); painter.scale(printer.resolution()/screenResolution, printer.resolution()/screenResolution); doc.drawContents(&painter); painter.restore(); painter.end(); 
+8
c ++ dpi qt pdf qtextdocument
source share
1 answer

QTextDocument uses its own drawing device for layout, which by default has a screen resolution.
You can change it with:

 doc.documentLayout()->setPaintDevice(&printer); // just before doc.setPageSize(printer.pageRect().size()); 
+7
source share

All Articles