Render QWidget to PDF with selectable text

415 views Asked by At

Assume a QWidget as a container for other widgets. The widgets in this container are a widget with layout QVBoxLayout that should show two columns of QLabel. After that there comes a new widget which is a QTableView.

I now would like to render the above described widget to a PDF file with selectable text. For now i set up my QPrinter and the correspoinding QPainter with

QPrinter* printer = new QPrinter(selectedPrinterInfo, QPrinter::ScreenResolution);
printer->setPageSize(QPageSize(QPageSize::A4));
printer->setOrientation(QPrinter::Portrait);
printer->setFullPage(true);
printer->setPageSize(QPrinter::A4);
printer->setOutputFormat(QPrinter::PdfFormat);
printer->setPageMargins(QMarginsF(15,15,15,15),QPageLayout::Unit::Millimeter);
printer->setOutputFileName(fileName);
printer->setResolution(300);

QPainter painter(printer);

now i render my widget with

myWidget->render(&painter);

this creates a nice PDF of a good quality, but it is only a image withing a pdf file. The text is not selectable. My approach for making it selectable was first render myWidget to QPicture and then replay it to QPainter

QPicture picture;
myWidget->render(&picture);
painter.drawPicture(0,0,picture);

Which creates a PDF that HAS selectable text. My problem with this approach QPicture has serious problems with correctly displaying this table. E.g. i set AlternatingRowColors to true and now the rowcolors overlap the grid lines. So is there any approach for rendering a QWidget directly with QPainter as PDF, or any other approach todo this without using QPicture

0

There are 0 answers